型号:
class Address
{
public string City { get; set; }
public string Zip { get; set; }
}
控制器:
[HttpPost]
public ActionResult GetAddress(Address model)
{
if (!String.IsNullOrEmpty(model.Zip))
{
model.City = GetCityByZip(model.Zip);
}
return View(model);
}
观点:
<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.Zip)
@Html.TextBoxFor(model => model.Zip)
@Html.ValidationMessageFor(model => model.Zip)
</div>
问题是每当城市被修改时,它永远不会反映在视图上。在调试期间,model.City
包含正确的值,但未显示在视图中。即使是简单的事情@Html.TextBoxFor(model => model.City)
也不会显示正确的model.City
值。