In my MVC application, I have a helper class designed to display a group of related buttons. I am trying to pass HTML attributes as an anonymous object:
new { @class="myClass1 myClass2" }
The helper passes the HTML as an MvcHtmlString , which I am currently creating as follows:
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 creates invalid HTML:
<button type='submit' name='Foo' { class = myClass1 myClass2 }>Bar</button>
Unfortunately, since it is passed to the helper as an object , I have no information about the type you are working with. I could have a ToString object and parse the result, but that seems pretty dumb. How can I programmatically convert an anonymous object to key="value" style HTML attributes? Is there an existing utility for this?
source share