html helper采用字典<string,object>,如何使用这个参数?

Bla*_*man 16 c# asp.net asp.net-mvc html-helper

如果html帮助器将idictionary作为参数,我该如何使用它?

我试过了:

<%= Html.Blah( new { id = "blah" }) %>
Run Code Online (Sandbox Code Playgroud)

但这不起作用.

Jus*_*ner 27

<%= Html.Blah( new Dictionary<string, object>(){
                                                   { "key", "value" },
                                                   { "key1", someObj },
                                                   { "blah", 1 }
                                               } );
Run Code Online (Sandbox Code Playgroud)


Jaa*_*ans 7

以防其他人遇到这个问题.存在一个将匿名对象转换为字典的辅助方法.

例如:

HtmlHelper.AnonymousObjectToHtmlAttributes( new { ng_model = "vm" } );
Run Code Online (Sandbox Code Playgroud)

换句话说,您可以根据OP的问题创建HTML帮助器方法,以获取对象并在其中使用此辅助方法.例如

public static string Blah(this HtmlHelper html, object htmlAttributes)
{
    // Create a dictionary from the object
    HtmlHelper.AnonymousObjectToHtmlAttributes( htmlAttributes );

    // ... rest of implementation
}
Run Code Online (Sandbox Code Playgroud)