2012-10-06 7 views
27

Frage: Ich brauche ein Dropdownlist wie diese zu erstellen:DropDownListFor mit einem benutzerdefinierten Attribut mit - in Attributname?

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

Jetzt kann ich hinzufügen, benutzerdefinierte Attribute wie folgt aus:

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

Leider, wenn es eine „-“ in der Variablenname, dann kompiliert es nicht.

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

Und was ist mit dem Multiple, das keinen Attributwert hat?

Wenn ich ein Wörterbuch oder eine NameValueColletion anstelle des new { @disabled = "disabled" } passieren, dann gibt es die Eigenschaften des NameValueColletion/Wörterbuch ...

Wie kann ich pass Attribute mit Sonderzeichen in der Attributschlüssel?

+0

möglich duplicate von [Silbentrennung html-attribute mit asp.net mvc] (http://stackoverflow.com/questions/2897733/hyphenated-html-attributes-with-asp-net-mvc) –

Antwort

59

einen Unter Verwenden Sie stattdessen

@data_placeholder = "whatever" 

Seit MVC3 "_" ersetzt wird durch "-" beim Rendern.

Dies funktioniert gut, da Unterstriche in Attributnamen in HTML nicht akzeptabel sind.

3

Ah, es ist einfach.
Der Fehler war, ein Wörterbuch von <string, string> anstelle eines Wörterbuchs von <string, object> (und verwenden Variablen anstelle von Eigenschaften in cOption) zu erklären ...


Mit Wörterbuch von <string, string> es das Objekt „ParamList“ verwendet Überlast statt 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>(); 

    } 

oder mehr 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); 
} 
+0

Das ist lustig, ich habe noch nie wendete es auf diese Weise an. Cool! – Jowen

Verwandte Themen