gis*_*per 2 .net c# asp.net-mvc
我创建了一个HTML Helper扩展,它在MVC中调用编辑模板部分视图(MyView).
我通过对象htmlAttributes参数将其他HTML属性传递给HTML Helper扩展.在HTML Helper扩展中,htmlAttributes被转换为RouteValueCollection(可以在这里使用IDictionary)并存储在ModelProperty对象中:
public static MvcHtmlString TextBoxFor(this HtmlHelper html, ModelProperty prop, object htmlAttributes)
{
prop.ControlHtmlAttributes = HtmlHelper.AnonymousObjectToHtmlAttributes(htmlAttributes);
return PartialExtensions.Partial(html, "MyView", prop);
}
Run Code Online (Sandbox Code Playgroud)
在'MyView'部分视图中,我想渲染控件并使用传递的HTML属性,因此我调用:
Html.TextArea(Model.ControlName, Model.Value, Model.ControlHtmlAttributes);
Run Code Online (Sandbox Code Playgroud)
但是这个dosnt的工作原理是因为第3个参数应该是'object htmlAttributes'如何将Model.ControlHtmlAttributes转换为对象htmlAttibutes?
无论如何要回答你的问题:
public static object ToAnonymousObject(this IDictionary<string, object> @this)
{
var expandoObject = new ExpandoObject();
var expandoDictionary = (IDictionary<string, object>) expandoObject;
foreach (var keyValuePair in @this)
{
expandoDictionary.Add(keyValuePair);
}
return expandoObject;
}
Run Code Online (Sandbox Code Playgroud)