Using MVC 3 RTM I\'m getting a strange NullReferenceException:
@helper TestHelperMethod() {
var extra = \"class=\\\"foo\\\"\";
That's a known limitation of those helpers. One possibility is to pass it as parameter:
@helper TestHelperMethod(HtmlHelper html) {
var extra = "class=\"foo\"";
}
Another possibility is to write the helper as an extension method:
public static class HtmlExtensions
{
public static MvcHtmlString TestHelperMethod(this HtmlHelper)
{
var div = new TagBuilder("div");
div.AddCssClass("foo");
return MvcHtmlString.Create(div.ToString());
}
}
and then:
@Html.TestHelperMethod()