In my MVC application, I have a helper class designed to render a group of related buttons. I’m trying to pass in the buttons’ HTML attributes as an anonymous object:
new { @class="myClass1 myClass2" }
The helper emits HTML as an MvcHtmlString, which I’m currently building like this:
foreach (var b in _buttons)
{
sb.AppendFormat("<button type='submit' name='{0}' {1}>{2}</button>",
b.Value,
b.HtmlAttributes,
b.Text);
}
My problem is that the above code produces invalid HTML:
<button type='submit' name='Foo' { class = myClass1 myClass2 }>Bar</button>
Unfortunately, since it’s passed to the helper as an object, I don’t have type information to work with. I could ToString the object and parse the result, but that seems pretty silly. How can I programmatically convert an anonymous object into key="value" style HTML attributes? Is there an existing utility for this?
You should use the
TagBuilderclass, which builds HTML tags for you.You can then write