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) {
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
source share