Asp.net mvc 如何将匿名对象HTML属性转换为字典<;字符串,对象>;

Asp.net mvc 如何将匿名对象HTML属性转换为字典<;字符串,对象>;,asp.net-mvc,html-helper,Asp.net Mvc,Html Helper,我为RadioButton提供了一个额外的重载,并希望向传入的HTML属性添加一个键值对 作为一个例子,我传递了如下内容: new { id = "someID" } 当我使用HtmlHelper.AnonymousObjectToHtmlAttributes方法时(我发现的建议似乎是这样的),它会生成一个包含4个项的字典,其中的键为“Comparer”、“Count”、“Keys”、“Values”。然后,我尝试使用反射来迭代“键”和“值”中的值,但也无法使其工作 基本上,我想要做的就是能够

我为RadioButton提供了一个额外的重载,并希望向传入的HTML属性添加一个键值对

作为一个例子,我传递了如下内容:

new { id = "someID" }
当我使用HtmlHelper.AnonymousObjectToHtmlAttributes方法时(我发现的建议似乎是这样的),它会生成一个包含4个项的字典,其中的键为“Comparer”、“Count”、“Keys”、“Values”。然后,我尝试使用反射来迭代“键”和“值”中的值,但也无法使其工作

基本上,我想要做的就是能够将htmlAttributes强制转换到IDictionary,添加一个项,然后将其传递到常规的RadioButtonFor方法

编辑: 这就是我真正想做的。提供一个名为isDisabled的重载,以便能够设置单选按钮的禁用状态,因为直接使用HTML属性很难做到这一点,因为disabled=false stillr esults in disabled呈现给标记并禁用单选按钮

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);
    }
public static MvcHtmlString RadioButtonOn(此HtmlHelper HtmlHelper、表达式、对象值、bool isDisabled、对象htmlAttributes)
{
var linkAttributes=System.Web.Mvc.HtmlHelper.AnonymousObjectToHtmlAttributes(htmlAttributes);
字典htmlAttributesDictionary=新字典();
foreach(linkAttributes中的var a)
{
如果(a.Key.ToLower()!=“禁用”)
{
htmlAttributesDictionary.Add(a.Key,a.Value);
}
}
如果(已禁用)
{
添加(“禁用”、“禁用”);
}
返回InputExtensions.RadioButtonOn(htmlHelper、表达式、值、htmlAttributesDictionary);
}

看起来您可能将AnonymousObjectToHtmlatAttributes应用了两次或错误的项

如果没有更多的代码,很难判断

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
编写重载时,请确保参数为:
objecthtmlattributes
,用于
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);
}
Public静态MvcHtmlString MyRadioButton(…,对象htmlAttributes)
{
返回MyRadioButton(..,HtmlHelper.AnonymousObjectToHtmlAtrBites(htmlAttributes);
}
公共静态MvcHtmlString MyRadioButton(…,IDictionary htmlAttributes)
{
htmlAttributes。添加(“项目”,项目);
返回RadioButton(…,htmlAttributes);
}

(需要明确的是,永远不要使用My…-这只是为了说明)

不清楚您为什么不使用接受
对象HtmlatAttribute的现有重载来添加
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);
}
public static MvcHtmlString RadioButtonOn(此HtmlHelper HtmlHelper、表达式、对象值、bool isDisabled、对象htmlAttributes)
{
IDictionary attributes=HtmlHelper.AnonymousObjectToHtmlAttributes(htmlAttributes);
如果(isDisabled&!attributes.ContainsKey(“disabled”))
{
添加(“禁用”、“禁用”);
}
返回InputExtensions.RadioButtonOn(htmlHelper、表达式、值、属性);
}

RadioButtonOn()
已经有一个重载来添加html属性。您实际想做什么。显示方法的代码。在视图中用实际代码更新问题var isDisabled=false;html.RadioButtonOn(model=>model.IsAircraftAndNotAsset,“true”,new{id=“rdoIsAircraftAndNotAssetYes”,disabled=isDisabled})仍然会导致收音机被禁用。我找不到任何其他方法来解决这个问题,当然了-
disabled=“disabled”
disabled=“true”
disabled=“false”
disabled=“anywhere”
都设置了disabled属性(即使第一个属性是无效的html)。这就是为什么我在我的第一条评论中问你实际想做什么。我试图根据模型的属性设置单选按钮的禁用状态。你可以在普通html帮助程序中使用条件
?:
,但是如果你经常使用,那么定制帮助程序是个好主意。
!attributes.ContainsKey(我的答案中的“disabled”)
可能会被删除,因为您无论如何都不会添加它,但如果您没有添加任何其他属性,并且实际传递最后一个参数的
null
(在这种情况下,构建一个新的
字典
),您可能需要添加对
htmlAttributes==null
的检查