Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
DropDownListFor binding
Message
De
14/01/2013 13:24:03
 
 
À
Tous
Information générale
Forum:
ASP.NET
Catégorie:
MVC
Titre:
DropDownListFor binding
Versions des environnements
Environment:
C# 5.0
Divers
Thread ID:
01562476
Message ID:
01562476
Vues:
54
Given:
 public class LLProduct
  {
    public Guid Id { get; set; }
    public String Name { get; set; }
  }

  public class ProductsViewModel
  {
    public LLProduct Product { get; set; }
  }
and an MVC action
      Guid id = Guid.NewGuid();
      List<LLProduct> list = new List<LLProduct>
        {
          new LLProduct{Id=Guid.NewGuid(),Name="One"},
          new LLProduct{Id = id,Name = "Two"}
        };
      ViewBag.Selector = new SelectList(list, "Id", "Name");

      var p = new LLProduct();
      p.Id = id;
 
      ProductsViewModel pvm = new ProductsViewModel{Product = p};
      return View(pvm);
then '@Html.DropDownListFor(x=>x.Product.Id,(SelectList)ViewBag.Selector)
' selects the right item ("Two")

If, however, I use 'p' as the parameter to the view (and change the model in the cshtml) then '@Html.DropDownListFor(x=>x.Id,(SelectList)ViewBag.Selector) shows "One" (no value selected in the generated html) ?
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform