Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
DropDownListFor binding
Message
From
15/01/2013 02:44:21
 
General information
Forum:
ASP.NET
Category:
MVC
Environment versions
Environment:
C# 5.0
Miscellaneous
Thread ID:
01562476
Message ID:
01562529
Views:
36
>>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.......


,
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform