【问题标题】:Create a checkbox ist control by extending html helper in mvc 3?通过在 mvc 3 中扩展 html 助手来创建一个复选框 ist 控件?
【发布时间】:2012-11-19 01:24:32
【问题描述】:

我正在尝试使用扩展的 html 助手创建动态复选框列表控件?单选按钮列表将由项目列表填充?不知道如何使任何列表项更灵活。

public static string CheckBoxList(this HtmlHelper helper, string name, <Some List> items)
        {
            var output = new StringBuilder();
            output.Append(@"<div class=""checkboxList"">");

            foreach (var item in items)
            {
                output.Append(@"<input type=""checkbox"" name=""");
                output.Append(name);
                output.Append("\" value=\"");
                output.Append(item.Value);
                output.Append("\"");

                output.Append(" />");
                output.Append(item.Value);
                output.Append("<br />");
            }

            output.Append("</div>");

            return output.ToString();
        }

【问题讨论】:

    标签: asp.net-mvc dynamic controls html-helper


    【解决方案1】:

    如何将值属性名称作为参数传入,然后通过反射获取该属性?

    var itemValue = item.GetType( ).GetProperty( propName ).GetValue( item, null );
    

    所以你的方法签名是这样的:

    public static string CheckBoxList(this HtmlHelper helper, string name, List<object> items, string propName)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-03-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-08-04
      • 1970-01-01
      相关资源
      最近更新 更多