TagBuilder add attribute without value
Same question from here.
How to add an attribute without value
This time, I want to create an input with an AngularJS attribute to use as a directive.
This is what I want:
<input ng-angular-abc />
This is what TagBuilder generated:
<input ng-angular-abc=""/>
I tried this, but none works:
tagBuilder.MergeAttribute("ng-angular-abc", "");
tagBuilder.MergeAttribute("ng-angular-abc", null);
tagBuilder.MergeAttribute("ng-angular-abc", "ng-angular-abc");
+3
source to share
2 answers
This is not exactly what you need, but you can create a helper method to create tags -
namespace YourNamespace
{
public static class CustomHelpers
{
public static MvcHtmlString TextboxWithProperty(this HtmlHelper html, string property)
{
StringBuilder result = new StringBuilder();
result.AppendFormat("<input {0} />", property);
return MvcHtmlString.Create(result.ToString());
}
}
}
and call it in your view -
@using YourNamespace
...
...
...
@Html.TextboxWithProperty("ng-angular-abc")
+1
source to share
An alternative approach is to use the Razor helpers that create a Helpers.cshtml file in the directory App_Code
. Your assistant would like this
@helper InputNg() { <input ng-angular-abc /> }
To access the shaver mode helpers just use the filename followed by the method as shown below:
@Helpers.InputNg()
This gives you much better control over your HTML.
+1
source to share