ASP.NET MVC: View does not get rendered with updated model value
I'm experiencing the a challenge in populating the child records. My previous code was like -
<%= Html.TextBox("DyeOrder.Summary[" + i + "].Ratio", Model.DyeOrder.Summary[i].Ratio.ToString("#0.00"), ratioProperties) %>
This code does not render the updated values after post back. To resolve this issue my work around was like 开发者_StackOverflow-
<%= "<input id='DyeOrder_Summary_" + i + "__Ratio' name='DyeOrder.Summary[" + i + "].Ratio' value='" + Model.DyeOrder.Summary[i].Ratio.ToString("#0.00") + "' " + ratioCss + " type='text' />"%>
This is very clumsy to me. Is there any better ideas...
As long as you do a
return View(YourUpdatedModel);
after your "postback" (I wouldn't use this term in MVC), it should be fine.
Just remember that the Model you return to the view must have the updated values.
When you post to a controller, unlike asp.net webforms, there is no ViewState or anything similar. You receive values in the controller, you do things with them, and you return a new view.
The new view might have the same model the controller received as a parameter (for example, if there were any validation errors), but it's not required. If the updates to the model were not done to the object variable associated with it (as it seems to be your case), you need to update the object you'll return with the view, or create a new one with the correct values and return that one.
What about:
<%= Html.TextBox(string.Format("DyeOrder_Summary_{0}__Ratio", i), ...) %>
精彩评论