Hi,
I have a View class that contains a list, this list explains the available files that the user have uploaded (rendered with an html helper).
To maintain this data on submit I have added the following to the view :
<%: Html.HiddenFor(model => model.ModelView.Files)%>
I was hoping that the mode.ModelView.Files list would be returned to the action on submit but it is not?
Is it not possible to have a list as hiddenfield?
More information : The user submit a couple of files that is saved on the service, when saved thay are refered to as GUID and is this list that is sent back to the user to render the saved images. The user makes some changes in the form and hit submit again the image list will be empty when getting to the control action, why?
BestRegards
The value of the Hidden Field created using Html.HiddenFor helper function is read using Model class object while the value of Hidden Field created using Html.Hidden helper function is read using Request.Form collection in ASP.Net MVC Razor.
The hidden field can also be added declaratively by dragging and dropping the hidden field control from asp.net toolbox. 6. The values for the properties can be set from the properties window as: 7. Add a label to the code where the value of the hidden field will be displayed. 8.
Here, the hidden field's name will be " HDName ", and the value of the hidden field will be " Saineshwar ". Generally strongly typed helper will contain 2 parameters like as shown below. Hidden Field Name which is Model Property. Value of Hidden Field (If we want to set value from the view that we can set this way).
To assign value, we added the Index Action Method (HttpPost). Following is the debug view of the index Action Method after assigning value to the hidden field from the controller. Following is the way to assign value to hidden fields from JavaScript. Following is the way to read the values of hidden fields from JavaScript.
Is it not possible to have a list as hiddenfield?
Of course that it is not possible. A hidden field takes only a single string value:
<input type="hidden" id="foo" name="foo" value="foo bar" />
So if you need a list you need multiple hidden fields, for each item of the list. And if those items are complex objects you need a hidden field for each property of each item of the list.
Or a much simpler solution is for this hidden field to represent some unique identifier:
<input type="hidden" id="filesId" name="filesId" value="123" />
and in your controller action you would use this unique identifier to refetch your collection from wherever you initially got it.
Yet another possibility is to persist your model into the Session
(just mentioning the Session for the completeness of my answer sake, but it's not something that I would actually recommend using).
Before I start I'd just like to mention that this is an example of one of the proposed solutions that was marked as the answer. Darrin got it right, here's an example of an implementation of the suggested solution...
I had a similar problem where I needed to store a generic list of type int in a hiddenfield. I tried the standard apporach which would be:
<%: Html.HiddenFor(foo => foo.ListOfIntegers) %>
That would however cause and exception. So I tried Darrin's suggestion and replaced the code above with this:
<%
foreach(int fooInt in Model.ListOfIntegers)
{ %>
<%: Html.Hidden("ListOfIntegers", fooInt) %>
<% } %>
This worked like a charm for me. Thanks Darrin.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With