Tag: mvc

MVC Generic Html by anonymous type

There is no equivalent for asp’s create generichtmlcontrol.
In some rare cases, it may make sense to do this. Esp when the generation of html is mixed with @ directives inside “” for attrs.

*Note that class is a keyword and cant be used as property name in the anonymouse type. So we use CssClass instead and make the Extension method aware of this possibility.

@Html.StartTag("div", new { id ="name", CssClass = "cls", style= @Model.Styles})

UPDATE (Feb 10th): You can say @class to use the reserved keyword. So the code atts[“class”] = atts[“CssClass”]; is redundant

Read more »