Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET MVC 2 and lists as Hidden values?

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

like image 872
Banshee Avatar asked Jan 31 '11 19:01

Banshee


People also ask

How to read the value of the Hidden Field in MVC?

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.

How to create a hidden field in ASP NET?

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.

What is the name and value of Hidden Field in helper?

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).

How to assign value to hidden fields in httppost?

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.


2 Answers

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).

like image 51
Darin Dimitrov Avatar answered Oct 24 '22 19:10

Darin Dimitrov


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.

like image 32
WernerVA Avatar answered Oct 24 '22 19:10

WernerVA