模型MVC模式不更新
class Address
{
public string Street { get; set; }
public string City { get; set; }
public string State { 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.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>
的問題是每當這個城市正在被修改,使它不會被反映在風景。在調試過程中,model.City
包含正確的值,但它不會顯示在視圖中。即使像@Html.TextBoxFor(model => model.City)
這樣簡單的東西也不會顯示正確的model.City
值。
你能後的視圖? – 2012-08-07 02:06:18
您發佈的模型和您作爲參數傳遞的模型不一樣。你引用了錯誤的模型嗎? – Tommy 2012-08-07 02:07:05
@MarkOreta:更新了問題,添加了視圖 – xar 2012-08-07 02:14:23