DropDownListFor with a custom attribute with - in attribute name?

46,647

Solution 1

Use an underscore instead

@data_placeholder = "whatever"

Since Mvc3 "_" is replaced with "-" when rendered. This applies to Mvc5 too.

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

Solution 2

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)...


With dictionary of <string, string> it uses the object "paramlist" overload, instead of 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:

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);
}
Share:
46,647
Stefan Steiger
Author by

Stefan Steiger

I'm an avid HTTP-header-reader, github-user and a few more minor things like BusinessIntelligence &amp; Web Software Developer Technologies I work with: Microsoft Reporting- &amp; Analysis Service (2005-2016), ASP.NET, ASP.NET MVC, .NET Core, ADO.NET, JSON, XML, SOAP, Thrift ActiveDirectory, OAuth, MS Federated Login XHTML5, JavaScript (jQuery must die), ReverseAJAX/WebSockets, WebGL, CSS3 C#, .NET/mono, plain old C, and occasional C++ or Java and a little Bash-Scripts, Python and PHP5 I have a rather broad experience with the following relational SQL databases T-SQL PL/PGsql including CLR / extended stored procedures/functions Occasionally, I also work with MySQL/MariaDB Firebird/Interbase Oracle 10g+ SqLite Access I develop Enterprise Web-Applications (.NET 2.0 &amp; 4.5) and interface to systems like LDAP/AD (ActiveDirectory) WebServices (including WCF, SOAP and Thrift) MS Federated Login OAuth DropBox XML &amp; JSON data-stores DWG/SVG imaging for architecture In my spare-time, I'm a Linux-Server-Enthusiast (I have my own Web &amp; DNS server) and reverse-engineer with interest in IDS Systems (IntrusionDetection), WireShark, IDA Pro Advanced, GDB, libPCAP. - Studied Theoretical Physics at the Swiss Federal Institute of Technology (ETHZ).

Updated on July 12, 2022

Comments

  • Stefan Steiger
    Stefan Steiger almost 2 years

    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...

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