ASP.NET MVC: Hidden Field Value Not Displayed Using HtmlHelper.Hidden

Something rather strange happens with my application:

I have the following property in my ViewModel:

public int? StakeholderId { get; set; } 

It is partially displayed as follows:

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

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

The problem I am facing is that the hidden field has nothing in its "value" attribute, which is displayed a second time, even if the value of StakeholderId now has a value.

If I simply output the value myself, it is displayed on the page, so I got it to display the value by doing the following:

 <input type="hidden" id="StakeholderId" name="stakeholderId" value="<%: Model.StakeholderId %>" /> 

But it is rather strange that the assistant does not raise the updated value?

(I use jQuery to submit forms and render the results of actions in divs, but I checked, and the html that I return is already wrong before jQuery does something with this, so I don't think it has much to do )

UPDATE

Since then, I have found that I can also clear the corresponding ModelState key before the action of my controller returns a partial view.

+67
c # asp.net-mvc html-helper
Jan 07
source share
2 answers

The helper will first search for the POSTED values ​​and use them. When you submit the form, it takes away the old identifier value. Your workaround is correct.

+58
Jan 07 '10 at 8:52
source share

ADDENDUM: several HTML forms, e.g. in a grid

As an addition to this problem, you need to be very careful with several forms on the same page, for example, in a grid, say, generated using Ajax.BeginForm.

You may be tempted to write something line by line:

 @foreach (var username in Model.TutorUserNames) { <tr> <td> @Html.ActionLink(username, MVC.Admin.TutorEditor.Details(username)) </td> <td> @using (Ajax.BeginForm("DeleteTutor", "Members", new AjaxOptions { UpdateTargetId = "AdminBlock", OnBegin = "isValidPleaseWait", LoadingElementId = "PleaseWait" }, new { name = "DeleteTutorForm", id = "DeleteTutorForm" })) { <input type="submit" value="Delete" /> @Html.Hidden("TutorName", username) } </td> </tr> } 

The lethal line is here:

 @Html.Hidden("TutorName", username) 

... and intend to use TutorName as an action parameter. EG:

 public virtual ActionResult DeleteTutor(string TutorName){...} 

If you do this, the unpleasant surprise you are participating in is that Html.Hidden ("TutorName", username), as Darin Dimitrov explains, displays the last POSTED value. That is, regardless of your cycle, ALL elements will be displayed with the TutorName of the last deleted Tutor!

The word around, in Razor syntax, should replace the @ Html.Hidden call with an explicit input tag:

 <input type="hidden" id="TutorName" name="TutorName" value='@username' /> 

This works as expected.

Those:

NEVER, NEVER USE Html.Hidden TO REVIEW A PARAMETER TO RETURN TO YOUR ACTIONS WHEN YOU USE MULTIPLE FORMS IN A NETWORK !!!

Final warning:

When building a hidden input tag, you need to specify a name and identifier, set the same value, otherwise at the time of writing (February 2011), this will not work correctly. Of course not in Google Chrome. All you get is returning a null parameter if you only have the identifier and attribute of the name.

+33
Feb 01 2018-11-11T00:
source share



All Articles