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)
@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.......