在属性名 - 与自定义属性DropDownListFor?(DropDownListFor with

2019-08-01 05:01发布

问:我需要建立这样一个下拉列表:

<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" })

而关于多,它没有属性值是什么?

如果我通过一个字典或一个NameValueColletion代替new { @disabled = "disabled" }然后将其输出的NameValueColletion /词典的属性...

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

Answer 1:

使用下划线代替

@data_placeholder = "whatever"

由于MVC3“_”替换为“ - ”渲染时。

作为底线不是在HTML属性名接受这工作得很好。



Answer 2:

嗯,这很容易。
这个错误是声明的字典<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);
}


文章来源: DropDownListFor with a custom attribute with - in attribute name?