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.......
var ClientName; ClientName =$('.trSelected td:eq(2)').text(); var url = '/Client/GetDetails/' ; $.getJSON(url, id, function (data) { // setFormControls(data.Id, data.Role, data.Location, data.JobType, data.Description); });The problem now is that I can not directly use setFormControls as the above because my form is much more complex and I don't even expose Ids for controls. This is my view:
@using WebDemo.Helper @model CardNumbers.Models.ClientViewModel @using (Html.BeginForm(null, null, FormMethod.Post, new { id = "sform", title = "Client Info" })) { <fieldset> <legend>Client Info</legend> @Html.ValidationSummary(true) @Html.HiddenFor(m => m.ClientId) @Html.EditorFor(m => m.Number, EditorTemplate.TextBox) @Html.EditorFor(m => m.Name, EditorTemplate.TextBox) @Html.EditorFor(m => m.Client.Address, EditorTemplate.EditBox) <div id="ContactsInfo"> <div id="Contact1"> @Html.EditorFor(m => m.Client.Contact1) </div> <div id="Contact2"> @Html.EditorFor(m => m.Client.Contact2) </div> </div> <div id="SaveCancel" class="float-right"> <button id="btnSave">Save</button> <button type="reset" id ="btnCancel" name="Reset">Cancel</button> </div> </fieldset> }So, do you see how can I associate returned json data with that view?