如何将匿名对象 HTML 属性转换为字典 <string, object>
How can I convert anonymous object HTML attributes to a Dictionary<string, object>
我正在为 RadioButtonFor 提供额外的重载,并希望将键值对添加到传入的 HTML 属性中。
作为一个例子,我传递了类似的东西:
new { id = "someID" }
当我使用 HtmlHelper.AnonymousObjectToHtmlAttributes 方法时,这似乎是我正在寻找的建议),它会生成一个包含 4 个项目的字典,键为 "Comparer"、"Count" , "Keys", "Values"。然后我尝试使用反射来迭代 "Keys" 和 "Values" 中的值,但也无法使其工作。
基本上我想做的就是能够将 htmlAttributes 转换为 IDictionary ,添加一个项目,然后将其传递给常规的 RadioButtonFor 方法。
编辑:
这是我真正想做的。提供一个名为 isDisabled 的重载,以便能够设置单选按钮的禁用状态,因为这不能直接使用 HTML 属性轻松完成,因为 disabled = false 仍然会导致禁用被渲染到标记并禁用单选按钮。
public static MvcHtmlString RadioButtonFor<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> expression, object value, bool isDisabled, object htmlAttributes)
{
var linkAttributes = System.Web.Mvc.HtmlHelper.AnonymousObjectToHtmlAttributes(htmlAttributes);
Dictionary<string, object> htmlAttributesDictionary = new Dictionary<string, object>();
foreach (var a in linkAttributes)
{
if (a.Key.ToLower() != "disabled")
{
htmlAttributesDictionary.Add(a.Key, a.Value);
}
}
if (isDisabled)
{
htmlAttributesDictionary.Add("disabled", "disabled");
}
return InputExtensions.RadioButtonFor<TModel, TProperty>(htmlHelper, expression, value, htmlAttributesDictionary);
}
看起来您可能将 AnonymousObjectToHtmlAttributes 应用了两次或应用到了错误的项目。
没有你的更多代码,很难说
var attributes = HtmlHelper.AnonymousObjectToHtmlAttributes(new { id = "someID" });
attributes.Count = 1
attributes.Keys.First() = id
与
相比
var attributes = HtmlHelper.AnonymousObjectToHtmlAttributes(HtmlHelper.AnonymousObjectToHtmlAttributes(new { id = "someID" }));
attributes.Count = 3
attributes.Keys.Join = Count,Keys,Values
在编写重载时,请确保您的参数是:object htmlAttributes
对于 new { }
部分和重载 IDictionary
,例如:
Public static MvcHtmlString MyRadioButtonFor(..., object htmlAttributes)
{
return MyRadioButtonFor(...., HtmlHelper.AnonymousObjectToHtmlAttrbites(htmlAttributes);
}
public static MvcHtmlString MyRadioButtonFor(..., IDictionary<string, object> htmlAttributes)
{
htmlAttributes.Add("item", item);
return RadioButtonFor(..., htmlAttributes);
}
(明确一点,永远不要使用我的... - 这只是为了说明)
不清楚您为什么不直接使用接受 object htmlAttributes
的现有重载来添加 disabled="disabled"
属性,但是以下应该有效
public static MvcHtmlString RadioButtonFor<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> expression, object value, bool isDisabled, object htmlAttributes)
{
IDictionary<string, object> attributes = HtmlHelper.AnonymousObjectToHtmlAttributes(htmlAttributes);
if (isDisabled && !attributes.ContainsKey("disabled"))
{
attributes.Add("disabled", "disabled");
}
return InputExtensions.RadioButtonFor<TModel, TProperty>(htmlHelper, expression, value, attributes);
}
我正在为 RadioButtonFor 提供额外的重载,并希望将键值对添加到传入的 HTML 属性中。
作为一个例子,我传递了类似的东西:
new { id = "someID" }
当我使用 HtmlHelper.AnonymousObjectToHtmlAttributes 方法时,这似乎是我正在寻找的建议),它会生成一个包含 4 个项目的字典,键为 "Comparer"、"Count" , "Keys", "Values"。然后我尝试使用反射来迭代 "Keys" 和 "Values" 中的值,但也无法使其工作。
基本上我想做的就是能够将 htmlAttributes 转换为 IDictionary ,添加一个项目,然后将其传递给常规的 RadioButtonFor 方法。
编辑: 这是我真正想做的。提供一个名为 isDisabled 的重载,以便能够设置单选按钮的禁用状态,因为这不能直接使用 HTML 属性轻松完成,因为 disabled = false 仍然会导致禁用被渲染到标记并禁用单选按钮。
public static MvcHtmlString RadioButtonFor<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> expression, object value, bool isDisabled, object htmlAttributes)
{
var linkAttributes = System.Web.Mvc.HtmlHelper.AnonymousObjectToHtmlAttributes(htmlAttributes);
Dictionary<string, object> htmlAttributesDictionary = new Dictionary<string, object>();
foreach (var a in linkAttributes)
{
if (a.Key.ToLower() != "disabled")
{
htmlAttributesDictionary.Add(a.Key, a.Value);
}
}
if (isDisabled)
{
htmlAttributesDictionary.Add("disabled", "disabled");
}
return InputExtensions.RadioButtonFor<TModel, TProperty>(htmlHelper, expression, value, htmlAttributesDictionary);
}
看起来您可能将 AnonymousObjectToHtmlAttributes 应用了两次或应用到了错误的项目。
没有你的更多代码,很难说
var attributes = HtmlHelper.AnonymousObjectToHtmlAttributes(new { id = "someID" });
attributes.Count = 1
attributes.Keys.First() = id
与
相比var attributes = HtmlHelper.AnonymousObjectToHtmlAttributes(HtmlHelper.AnonymousObjectToHtmlAttributes(new { id = "someID" }));
attributes.Count = 3
attributes.Keys.Join = Count,Keys,Values
在编写重载时,请确保您的参数是:object htmlAttributes
对于 new { }
部分和重载 IDictionary
,例如:
Public static MvcHtmlString MyRadioButtonFor(..., object htmlAttributes)
{
return MyRadioButtonFor(...., HtmlHelper.AnonymousObjectToHtmlAttrbites(htmlAttributes);
}
public static MvcHtmlString MyRadioButtonFor(..., IDictionary<string, object> htmlAttributes)
{
htmlAttributes.Add("item", item);
return RadioButtonFor(..., htmlAttributes);
}
(明确一点,永远不要使用我的... - 这只是为了说明)
不清楚您为什么不直接使用接受 object htmlAttributes
的现有重载来添加 disabled="disabled"
属性,但是以下应该有效
public static MvcHtmlString RadioButtonFor<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> expression, object value, bool isDisabled, object htmlAttributes)
{
IDictionary<string, object> attributes = HtmlHelper.AnonymousObjectToHtmlAttributes(htmlAttributes);
if (isDisabled && !attributes.ContainsKey("disabled"))
{
attributes.Add("disabled", "disabled");
}
return InputExtensions.RadioButtonFor<TModel, TProperty>(htmlHelper, expression, value, attributes);
}