views:

1788

answers:

2

Hey,

Something pretty weird is happening with my app:

I have the following property in my ViewModel:

public int? StakeholderId { get; set; }

It gets rendered in a partial view as follows:

<%= Html.Hidden("StakeholderId", Model.StakeholderId) %>

The form is submitted, and the relevant controller action generates an id and updates the model, before returning the same view with the updated model

The problem I'm experiencing is that the hidden field does not have anything in its "value" attribute rendered the second time even though StakeholderId now has a value.

If I just output the value on its own, it shows up on the page, so I've got it to render the value by doing this:

<input type="hidden" id="StakeholderId" name="stakeholderId" value="<%= Html.Encode(Model.StakeholderId) %>" />

But it's pretty strange that the helper doesn't pick up the updated value?

(I'm using jQuery to submit forms and render the action results into divs, but I've checked and the html I get back is already wrong before jQuery does anything with it, so I don't think that has much to do with anything)

+5  A: 

The helper will first look for POSTed values and use them. As you are posting the form it will pick up the old value of the ID. Your workaround is correct.

Darin Dimitrov
Thanks, that's pretty counter-intuitive though isn't it? If I pass a concrete value in the value parameter, surely I want it to be emitted otherwise I would omit it altogether? I wonder what the reason for this design is...
Veli
A: 

I'm experiencing the same challenge populating the child records. My previous code was like -

<%= Html.TextBox("DyeOrder.Summary[" + i + "].Ratio", Model.DyeOrder.Summary[i].Ratio.ToString("#0.00"), ratioProperties)  %>

and I changed it like -

<%= "<input id='DyeOrder_Summary_" + i + "__Ratio' name='DyeOrder.Summary[" + i + "].Ratio'  value='" + Model.DyeOrder.Summary[i].Ratio.ToString("#0.00") + "' " + ratioCss + "  type='text' />"%>

Any idea to resolve this issue?

Newton
I'd suggest asking this as its own question. it looks like your problem is the ID - missing brackets, too many underscores...
Veli