My view displays a series of rows in an html table. The rows are ordered by a rank order from the database. One of the columns in the html table is a textbox that will allow the user to modify this rank order. There is an Update submit button at the bottom of the table which will allow the user to save this new order. The problem Im encountering is that the rank order is updated correctly to the database, but when the controller returns back to the view, all data is updated correctly on the screen but the values in the text box are not updated.
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)
My View Code
@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>
}
Controller code:
[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);
}