views:

45

answers:

2

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 -

<%= "<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...

A: 

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.

salgiza
Hi salgiza,Thanks for your response.Though my work around is working fine, Still I want it in a right way. Lets review my case again.As I post data to the controller I get the model and I do whatever I need to; then return the model with the view. The problem is view does not show the updated value it shows the old ones. Even, while I'm debugging the view it shows the new value but the htmlhelper does not generate the html with new value.This problem does not occur always, it occurs only if you use parent-child records in a single view.Is there anything I'm missing or it's bug.
Newton
It's certainly very weird. In the end, the View is a totally new object, so everything that you are using when rendering it (including the Html helpers, which are static methods) don't know anything about the original values. The only info available to the view is the Model and the ViewData dictionary. If by debugging you can attest that the values before you return the View are correct, might it be your browser the source of the problem? You are rendering inputs, and the browser might be storing the values for autocomplete or something similar...
salgiza
BTW, I can't check it right now, but I would think that you can add breakpoints to the view. It might be a good way to check where those rogue values are coming from.
salgiza
A: 

What about:

<%= Html.TextBox(string.Format("DyeOrder_Summary_{0}__Ratio", i), ...) %>
Neil T.