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(); myRepo.UpdateAutoPicks(...); } return View("Index", model); }
source share