Fill out the form based on the selection of the drop-down list (MVC 4)

I am using asp.net mvc 4 Is there a way to update the form based on the choices made by the user?

(in this case, I want to fill in the address fields, if something is selected in the drop-down list, otherwise I need to enter a new address)

My model: public class NewCompanyModel {

[Required] public string CompanyName { get; set; } public bool IsSameDayRequired { get; set; } public int AddressID { get; set; } public Address RegisterOfficeAddress { get; set; } } 

View:

 @model ViewModels.NewCompanyModel @using (Html.BeginForm(null, null, FormMethod.Post, new { name = "frm", id = "frm" })) { @Html.ValidationSummary(true) <fieldset id="test"> <legend>Company</legend> <h2>Register office address</h2> <div class="editor-label"> @Html.LabelFor(model => model.AddressID) </div> <div class="editor-field"> @Html.DropDownListFor(model => model.AddressID, (IList<SelectListItem>)ViewBag.Addresses, new {id = "address", onchange = "window.location.href='/wizard/Address?value=' + this.value;" }) </div> <div class="editor-label"> @Html.LabelFor(model => model.RegisterOfficeAddress.BuildingNameOrNumber) </div> <div class="editor-field"> @Html.EditorFor(model => model.RegisterOfficeAddress.BuildingNameOrNumber) @Html.ValidationMessageFor(model => model.RegisterOfficeAddress.BuildingNameOrNumber) </div> <div class="editor-label"> @Html.LabelFor(model => model.RegisterOfficeAddress.StreetName) </div> <div class="editor-field"> @Html.EditorFor(model => model.RegisterOfficeAddress.StreetName) @Html.ValidationMessageFor(model => model.RegisterOfficeAddress.StreetName) </div> 

and controller:

  public ActionResult Address(string value) { //get the address from db and somehow update the view } 

The question is how do you update 'model.RegisterOfficeAddress.StreetName', etc. Just to understand that this is just part of the form, so I can’t imagine it yet.

Thank you very much

+4
source share
2 answers

Thank you for your help; I decided to use a different approach: When changing the drop-down list, I will submit the form:

 <div class="editor-label"> @Html.LabelFor(model => model.ServiceAddress.AddressID) </div> <div class="editor-field"> @Html.DropDownListFor(model => model.ServiceAddress.AddressID, (IEnumerable<SelectListItem>)ViewBag.Addresses, new { onchange = "this.form.submit();" }) @Html.ValidationMessageFor(model => model.ServiceAddress.AddressID) </div> 

and then in the controller:

  [HttpPost] public ActionResult NewDirector(NewDirectorVM vm, string value) { ModelState.Clear(); if (vm.ServiceAddress.AddressID > 0) { //Updates the properties of the viewModel vm.ServiceAddress = _Repository.GetAddress(vm.ServiceAddress.AddressID); } return View("NewDirector", vm); } 

Pay attention to ModelState.Clear(); , which actually allows you to update the view from the controller (otherwise, all changes would make the viewModel the controller would be overwritten with values ​​in the view).

+2
source

A common way in such cases is to update other fields via javascript:

 $('#@Html.IdFor(model => model.AddressID)').on('change',function(){ $.get(...,function(data){ $('#@Html.IdFor(model => model.RegisterOfficeAddress.BuildingNameOrNumber)').val(data) }) }) 
+1
source

All Articles