DropDownListFor带有自定义属性 - 在属性名称中?

时间:2012-10-06 11:33:16

标签: c# asp.net-mvc asp.net-mvc-3 razor html.dropdownlistfor

问题:我需要创建一个这样的下拉列表:

<select id="ddCustomers" data-placeholder="Choose a customer" class="chzn-select" style="width:350px;" tabindex="1" multiple>

现在我可以添加如下自定义属性:

@Html.DropDownListFor(m => m.Id, Model.Values, new { @disabled = "disabled" })

不幸的是,如果变量名中有“ - ”,那么它就不会编译。

@Html.DropDownListFor(m => m.Id, Model.Values, new { @disabled = "disabled", @data-placeholder = "whatever" })

那么没有属性值的倍数呢?

如果我传递Dictionary或NameValueColletion而不是new { @disabled = "disabled" },那么它会输出NameValueColletion / Dictionary的属性......

如何在属性键中传递带有特殊字符的属性?

2 个答案:

答案 0 :(得分:66)

使用下划线

@data_placeholder = "whatever"

由于Mvc3“_”在渲染时被替换为“ - ”。

这很好用,因为html中的属性名称不接受下划线。

答案 1 :(得分:3)

啊,这很容易。
错误是宣布<string, string>字典 而不是<string, object>的字典(并使用变量代替cOption中的属性)...


使用<string, string>字典时,它使用对象“paramlist”重载,而不是IDictionary<string, object>;)

@Html.DropDownListFor(model => model.Title, new SelectList(Model.ls, "value", "text"), Model.nvc)

 <!--
 @Html.DropDownList("myIdAndName", new SelectList(Model.ls, "value", "text"), Model.nvc)
 -->




    public ActionResult Index()
    {
        cHomeModel HomeModel = new cHomeModel();

        HomeModel.nvc.Add("class", "chzn-select");
        HomeModel.nvc.Add("data-placeholder", "Choose a customer");
        HomeModel.nvc.Add("style", "width:350px;");
        HomeModel.nvc.Add("tabindex", "1");
        HomeModel.nvc.Add("multiple", "multiple");
        HomeModel.nvc.Add("id", "lol");


        cOption option = null;


        for (int i = 0; i < 10; ++i)
        {
            option = new cOption();

            option.value = i.ToString();
            option.text = "text" + i.ToString();

            HomeModel.ls.Add(option);
        }


        return View(HomeModel);
    }





    public class cOption
    {
        public string value
        {
            get;
            set;
        }

        public string text
        {
            get;
            set;
        }

    }


    public class cHomeModel
    {
        public string Title = "MyDropDownListName";
        public List<cOption> ls = new List<cOption>();


        public System.Collections.Generic.Dictionary<string, object> nvc = new System.Collections.Generic.Dictionary<string, object>();

    }

或更多Linqiq:

public ActionResult Index()
{
    cHomeModel HomeModel = new cHomeModel();

    HomeModel.nvc.Add("class", "chzn-select");
    HomeModel.nvc.Add("data-placeholder", "Choose a customer");
    HomeModel.nvc.Add("style", "width:350px;");
    HomeModel.nvc.Add("tabindex", "1");
    HomeModel.nvc.Add("multiple", "multiple");
    HomeModel.nvc.Add("id", "lol");


    HomeModel.ls = System.Linq.Enumerable.Range(0, 9)
            .Select(x => new cOption() { text = x.ToString(), value = x.ToString() })
            .ToList();


    // or otherwise: 
    HomeModel.ls = (
                 from i in System.Linq.Enumerable.Range(0, 9)
                 select new cOption() { text = i.ToString(), value = i.ToString() }
    ).ToList();


    return View(HomeModel);
}