40

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

<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 的属性...

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

4

2 回答 2

76

改用下划线

@data_placeholder = "whatever"

由于 Mvc3 的“_”在渲染时被替换为“-”。这也适用于 Mvc5。

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

于 2012-10-06T11:50:39.103 回答
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);
}
于 2012-10-08T07:20:54.920 回答