我正在尝试创建一个可以接受任意参数列表的 View 助手类,但试图在 View 端获得简单的语法。这是使用 MVC.NET 中的 Razor 引擎。我意识到我可以通过使用 ViewDataDictionary 来做到这一点,但我更喜欢匿名类型语法。
@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];
//}
}
最佳答案
使用System.Web.WebPages.Html.HtmlHelper.AnonymousObjectToHtmlAttributes(obj)
将匿名类型转换为RouteValueDictionary。
关于c# - 处理匿名类型的 MVC Helper 方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21264412/