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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12759396/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 00:20:58  来源:igfitidea点击:

DropDownListFor with a custom attribute with - in attribute name?

c#asp.net-mvcasp.net-mvc-3razorhtml.dropdownlistfor

提问by Stefan Steiger

Question: I need to create a dropdownlist like this:

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

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

Now I can add custom attributes like this:

现在我可以添加这样的自定义属性:

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

Unfortunately, if there is a "-" in the variable name, then it doesn't compile.

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

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

And what about the multiple, which has no attribute value ?

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

If I pass a Dictionary or a NameValueColletion instead of the new { @disabled = "disabled" }, then it outputs the properties of the NameValueColletion/Dictionary...

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

How can I pass attributes with special characters in the attribute key ?

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

采纳答案by dove

Use an underscore instead

改用下划线

@data_placeholder = "whatever"

Since Mvc3 "_" is replaced with "-" when rendered.

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

This works fine as underscores are not acceptable in attribute names in html.

这工作正常,因为在 html 中的属性名称中不接受下划线。

回答by Stefan Steiger

Ah, it's easy.
The mistake was to declare a dictionary of <string, string>instead of a dictionary of <string, object>(and to use variables instead of properties in cOption)...

啊,这很容易。
错误是声明了一个字典<string, string>而不是一个字典<string, object>(并在 cOption 中使用变量而不是属性)......


With dictionary of <string, string>it uses the object "paramlist" overload, instead of IDictionary<string, object>;)


使用<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>();

    }

or more Linqiq:

或更多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);
}