【问题标题】:How to add a htmlAttributes in this HtmlHelper?如何在这个 HtmlHelper 中添加 htmlAttributes?
【发布时间】:2013-01-14 04:52:48
【问题描述】:

我现在正在使用 this code 使用 MVC4 实现 RadioButtonList。

如您所见,该函数没有 htmlAttributes 参数。所以我想添加它,这就是问题所在。请检查 RadioButtonFor() 的 htmlAttributes 是否被 id 占用。

我试图添加它,但由于循环的 id 已经存在而引发错误。

public static class HtmlExtensions
{
    public static MvcHtmlString RadioButtonForSelectList<TModel, TProperty>(
        this HtmlHelper<TModel> htmlHelper,
        Expression<Func<TModel, TProperty>> expression,
        IEnumerable<SelectListItem> listOfValues)
    {
        return htmlHelper.RadioButtonForSelectList(expression, listOfValues, null);
    }

    public static MvcHtmlString RadioButtonForSelectList<TModel, TProperty>(
        this HtmlHelper<TModel> htmlHelper,
        Expression<Func<TModel, TProperty>> expression,
        IEnumerable<SelectListItem> listOfValues,
        object htmlAttributes)
    {
        return htmlHelper.RadioButtonForSelectList(expression, listOfValues, new RouteValueDictionary(htmlAttributes));
    }

    public static MvcHtmlString RadioButtonForSelectList<TModel, TProperty>(
        this HtmlHelper<TModel> htmlHelper,
        Expression<Func<TModel, TProperty>> expression,
        IEnumerable<SelectListItem> listOfValues,
        IDictionary<string, object> htmlAttributes)
    {
        var metaData = ModelMetadata.FromLambdaExpression(expression, htmlHelper.ViewData);
        var sb = new StringBuilder();
        if (listOfValues != null)
        {
            foreach (SelectListItem item in listOfValues)
            {
                var id = string.Format(
                    "{0}_{1}",
                    metaData.PropertyName,
                    item.Value
                );

                var radio = htmlHelper.RadioButtonFor(expression, item.Value, new { id = id }).ToHtmlString();
                sb.AppendFormat(
                    "{0}<label for=\"{1}\">{2}</label>",
                    radio,
                    id,
                    HttpUtility.HtmlEncode(item.Text)
                );
            }
        }
        return MvcHtmlString.Create(sb.ToString());
    }
}

【问题讨论】:

    标签: c# asp.net .net asp.net-mvc-4 html-helper


    【解决方案1】:

    在第三种方法中,传递给正在创建的单选按钮的 html 属性看起来是new { id = id }。尝试用方法中的参数替换它。

    更新

    在 html 属性中包含 id 并在每次循环迭代中为 id 分配一个新值。

    if (listOfValues != null)
    {
        if (!htmlAttributes.ContainsKey("id"))
        {
            htmlAttributes.Add("id", null);
        }
        foreach (SelectListItem item in listOfValues)
        {
            var id = string.Format(
                "{0}_{1}",
                metaData.PropertyName,
                item.Value
            );
            htmlAttributes["id"] = id;
            var radio = htmlHelper.RadioButtonFor(expression, item.Value, htmlAttributes).ToHtmlString();
            sb.AppendFormat(
                "{0}<label for=\"{1}\">{2}</label>",
                radio,
                id,
                HttpUtility.HtmlEncode(item.Text)
            );
        }
    }
    

    【讨论】:

    • 是的,但是删除了 id = id 并且我需要那个参数.. 没有办法加入他们吗?
    • 在我头上,您可以手动添加它们,例如:htmlAttributes.Add("id", id)。
    • 我在里面使用了一个循环..我需要克隆我想这样做的字典
    • 哦,是的,有一个循环,我错过了。在这种情况下,您可以在循环中的每次迭代中分配“id”键的值。更新了我的答案。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-03
    相关资源
    最近更新 更多