Model
class Address { public string Street { get; set; } public string City { get; set; } public string State { get; set; } public string Zip { get; set; } }
Controller action
[HttpPost] public ActionResult GetAddress(Address model) { if (!String.IsNullOrEmpty(model.Zip)) { model.City = GetCityByZip(model.Zip); } return View(model); }
View
<div class="formrow"> @Html.LabelFor(model => model.City) @Html.TextBoxFor(model => model.City) @Html.ValidationMessageFor(model => model.City) </div> <div class="formrow"> @Html.LabelFor(model => model.State) @Html.DropDownListFor(model => model.State, (IEnumerable<SelectListItem>)ViewBag.States, new { style = "width:217px;" }) @Html.ValidationMessageFor(model => model.State) </div> <div class="formrow"> @Html.LabelFor(model => model.Zip) @Html.TextBoxFor(model => model.Zip) @Html.ValidationMessageFor(model => model.Zip) </div>
The problem is that when a city changes, it never reflects on the view. During debugging, model.City contains the correct value, but it does not appear in the view. Even just as @Html.TextBoxFor(model => model.City) does not display the correct value of model.City .
source share