【发布时间】:2011-07-25 13:34:16
【问题描述】:
我的视图在 html 表中显示一系列行。这些行按数据库中的等级顺序排序。 html 表中的一列是一个文本框,允许用户修改此排名顺序。表格底部有一个更新提交按钮,允许用户保存这个新订单。我遇到的问题是排名顺序正确更新到数据库,但是当控制器返回视图时,屏幕上的所有数据都正确更新但文本框中的值没有更新。
Order | Data
================================================
10 | Item 1
20 | Item 2
30 | Item 3
Lets say the user makes the following changes
Order | Data
================================================
50 | Item 1
20 | Item 2
10 | Item 3
Now User hits update and what is rendered is:
Order | Data
================================================
50 | Item 3
20 | Item 2
10 | Item 1
(Note data was sorted correctly but values of TextBoxFor did not update)
我的查看代码
@using (Html.BeginForm("UpdateRanks", "MyController")) {
<table cellpadding="0" cellspacing="0" width="100%">
@for (int i = 0; i < Model.DbSelections.Count; i++) {
var item = Model.DbSelections[i];
<tr>
<td>
@Html.TextBoxFor(m => m.DbSelections[i].Rank, new { @class = "NarrowTextBox" })
</td>
<td>
@Model.DbSelections[i].Rank (works!), @item.ItemName<br />
</td>
</tr>
}
<input type="submit" value="Update" name="updateaction" class="StandardButton"/>
</table>
}
控制器代码:
[HttpPost]
[MultiButton(MatchFormKey = "updateaction", MatchFormValue = "Update")]
public ActionResult UpdateRanks(MyViewModel model) {
if (ModelState.IsValid) {
MyRepository myRepo = new MyRepository();
<!-- saves updated ranks to database - it works -->
myRepo.UpdateAutoPicks(...);
<!-- after saved ranks above, now reload data including ranks from database -->
<!-- debugging shows that data and ranks and ordering is correct->
model.DbSelections = myRepo.GetItems();
return View("Index", model);
} else {
<!-- blah -->
}
return View("Index", model);
}
【问题讨论】:
标签: asp.net-mvc-3 c#-4.0 razor