I want to generate depending on what I'm passing an specific attribute with his value. This is how I want to use the helper:
<sometag @PossibleHelper(parameter)/>
After PossibleHelper do his thing, this could be the result:
<sometag attributeName="attributeValue"/>
How can I express that in the helper?
@helper PossibleHelper(someType){
if(condition){
attributeName="attributeValue" //this is wrong
}
}
When you're in a helper it's just regular razor syntax.
@helper PossibleHelper(SomeType something) {
if (condition) {
<span attributeName="attributeValue">blah</span>
}
}
You can set an attribute like this.
@helper PossibleHelper(int something) {
var attrValue = string.Empty;
if (true) {
attrValue = "attributeValue";
}
@(string.Format("attribute={0}", attrValue))
}
Usage:
<sometag @PossibleHelper(parameter)/>
FYI, you can also make an extension method for HtmlHelper
which may be better if the code is shared between views or there is a decent amount of logic.
public static class HtmlHelperExtensions
{
public static MvcHtmlString SomeExtension(this HtmlHelper htmlHelper)
{
// ...
}
}