Reputation: 2236
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);
}
Upvotes: 4
Views: 18457
Reputation: 1
You do not have to clear the whole ModelState, just the one you wish to update... This may reduce unwanted side effects:
As an example, I have a section of code that formats the user's supplied phone number, not just validates it, to the US 10 digit format. This will return the "cleansed" value for this field on:
ModelState["Phone"].Value = new ValueProviderResult(dummyph, dummyph, System.Globalization.CultureInfo.CurrentUICulture);
Upvotes: 0
Reputation: 1256
I was having the same problem, and ModelState.Clear() in the controller solved this issue. However, I'm not sure whether it's the right thing to do.
Upvotes: 0
Reputation: 66
See if this other Post helps you out.
The one caveat with using this solution is that you'll lose any of the built in features of using TextBoxFor(), such as validation. If this doesn't work you can try doing ModelState.Clear() to clear out the posted values (see here for more details).
Upvotes: 4