gpt4 book ai didi

c# - MVC - 将 IDictionary 或 RouteValueDictionary 转换为对象 htmlAttributes

转载 作者:行者123 更新时间:2023-11-30 19:41:20 24 4
gpt4 key购买 nike

我创建了一个 HTML 帮助程序扩展,它在 MVC 中调用编辑器模板部分 View (MyView)。

我通过对象 htmlAttributes 参数将额外的 HTML 属性传递给 HTML Helper Extension。在 HTML Helper Extension 中,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);
}

在“MyView”部分 View 中,我想呈现控件并使用传递的 HTML 属性,所以我调用:

Html.TextArea(Model.ControlName, Model.Value, Model.ControlHtmlAttributes);

但是这不起作用,因为第三个参数应该是“对象 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;
}

关于c# - MVC - 将 IDictionary 或 RouteValueDictionary 转换为对象 htmlAttributes,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20075635/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com