2011-08-25 72 views
1

我正在顯示我的頁面上的數據集合。我爲每一行顯示標籤和文本框。MVC3:更新後的值不會反映在文本框後

下面是我查看示例代碼:

@using (Html.BeginForm()) 
{ 
    <input type="submit" value="Ok" /> 
    @for (int index = 0; index < Model.Persons.Count; index++) 
    { 
     @Model.Persons[index].Name 
     @Html.TextBoxFor(m => Model.Persons[index].Amount) 
    } 
} 

在POST操作,我更新量(說5增加)。

[HttpPost] 
    public ActionResult Persons(PersonList presenter) 
    { 

     for (int index = 0; index < presenter.Persons.Count; index++) 
     { 
      presenter.Persons[index].Amount += 5; 
     } 
     return View(presenter); 
    } 

此更新的金額未反映在頁面上。 但是,如果我用下面的代碼則正常顯示,像

@Model.Persons[index].Amount 

或者

<input type="text" [email protected]("Persons_" + index + "__Amount") name="Persons[@index].Amount " value="@Model.Persons[index].Amount" ></input> 

我想這樣的行爲爲什麼會發生的原因是什麼?

任何幫助,將不勝感激。

回答

2

發佈的值將覆蓋視圖模型值。我已經解決類​​似的問題在使用前...沿

PersonCollection persons = (PersonCollection)ModelState["Persons"].Value.RawValue; 
persons[index].Amount += 5; 
ModelState["Persons"].Value = new ValueProviderResult(persons, "Persons", CultureInfo.InvariantCulture); 
5

ModelState["Field"].Value = new ValueProviderResult("NewValue", "Field", CultureInfo.InvariantCulture); 

我猜你的情況(我不完全相信你的目標是什麼)事在更新值之前,只需添加ModelState.Clear(),更新您的集合並將其返回。

相關問題