2012-10-06 105 views
27

Pregunta: ¿Es necesario crear una lista desplegable de esta manera:DropDownListFor con un atributo personalizado con - en nombre de atributo?

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

Ahora puede añadir atributos personalizados como esto:

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

Desafortunadamente, si hay un "-" en el nombre de la variable, entonces no compila.

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

¿Y el múltiplo, que no tiene ningún valor de atributo?

Si paso un diccionario o una NameValueColletion en lugar de la new { @disabled = "disabled" }, entonces se da salida a las propiedades de la NameValueColletion/diccionario ...

¿Cómo puedo pasar atributos con caracteres especiales en la clave de atributo?

+0

posible duplicado de [Hyphenated html attributes with asp.net mvc] (http://stackoverflow.com/questions/2897733/hyphenated-html-attributes-with-asp-net-mvc) –

Respuesta

59

utilizar un guión bajo

@data_placeholder = "whatever" 

Desde MvC3 "_" se sustituye por "-" cuando se representa.

Esto funciona bien ya que los guiones bajos no son aceptables en los nombres de atributos en html.

3

Ah, es fácil.
El error fue declarar un diccionario de <string, string> en lugar de un diccionario de <string, object> (y utilizar variables en lugar de propiedades en cOption) ...


con el diccionario de <string, string> que utiliza el objeto "paramlist" sobrecarga, en lugar de 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>(); 

    } 

o más 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

Eso es gracioso, nunca aplicado de esta manera. ¡Guay! – Jowen

Cuestiones relacionadas