Razor MVC3 - TextBoxFor () values ​​bound to the view model are not updated correctly after returning from Post

In my view, a row of rows is displayed in the html table. Rows are ordered by ranking order from the database. One of the columns in the html table is a text field that allows the user to change this ranking order. At the bottom of the table there is a button “Refresh Gift”, which will allow the user to save this new order. The problem I am facing is that the order of the ranks is updated correctly in the database, but when the controller returns to the view, all the data is updated on the screen correctly, 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); } 
+4
source share
3 answers

See if this other Message helps.

One caveat using this solution is that you will lose any built-in functions using TextBoxFor (), such as validation. If this does not work, you can try making ModelState.Clear () to clear the published values ​​(see here for details).

+4
source

I had the same problem, and ModelState.Clear () in the controller solved this problem. However, I'm not sure if this is the right thing to do.

0
source

You do not need to clear the entire ModelState, only the one you want to update ... This can reduce unwanted side effects:

As an example, I have a section of code that formats a user-specified phone number, and not only confirms it, in a 10-digit US format. This will return a “cleared” value for this field:

ModelState ["Phone"]. Value = new ValueProviderResult (dummyph, dummyph, System.Globalization.CultureInfo.CurrentUICulture);

0
source

Source: https://habr.com/ru/post/1345770/


All Articles