Reputation: 1580
I'm trying to create a view Helper class that can take an arbitrary list of parameters, but trying to get a simple syntax on the view side. This is using the Razor engine in MVC.NET. I realize I can do this by using a ViewDataDictionary, however I prefer the Anonymous type syntax.
@Helper.CreateDiv(new {@class="myclass", @id="myId"})
public static string DivHelper(object values) {
string html = "<div";
//How do I iterate through the key/value pairs here?
//foreach(var key in values.Keys){
// html += String.format(" %s=\"%s\"",key,values[key];
//}
}
Upvotes: 2
Views: 416
Reputation: 141638
Use System.Web.WebPages.Html.HtmlHelper.AnonymousObjectToHtmlAttributes(obj)
to convert an anonymous type into a RouteValueDictionary.
Upvotes: 3