Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
DropDownListFor binding
Message
De
15/01/2013 02:44:21
 
 
Information générale
Forum:
ASP.NET
Catégorie:
MVC
Versions des environnements
Environment:
C# 5.0
Divers
Thread ID:
01562476
Message ID:
01562529
Vues:
34
>>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) ?
>
Hi

>What do you mean by p as the parameter to the view? What exactly is the problem - the list doesn't show both entries?

Above code with this razor view:
@model  ProductsViewModel

@using (Html.BeginForm("ProductLists", "Products"))
{
  @Html.DropDownListFor(x=>x.Product.Id,(SelectList)ViewBag.Selector)
}
Shows the list with "Two" as expected. Same controller code but with the return changed to
return View(p);
and view changed to:
@model  LLProduct

@using (Html.BeginForm("ProductLists", "Products"))
{
  @Html.DropDownListFor(x=>x.Id,(SelectList)ViewBag.Selector)
}
shows "One" - second item is not selected.......


,
Précédent
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform