Click here to Skip to main content
15,894,250 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

there is scenario i want to check the all the form controls values.

for this i was created the hidden field .

and was used the post method in view( @Html.Hidden("HProjectTypeId", Model.ProductTypeId);,


but in controller i was im getting null value..

im very new to mvc kindly guide me.

Regards
ruc.
Posted

1 solution

Well, hidden fields are just like other fields. Although it is better using the HiddenFor[^] method instead of Hidden[^] if possible, you should have a hidden field rendered with name HProjectTypeId and the value from Model.ProductTypeId. You should check in the browser, to see it for yourself.
But, what you get back in the action depends on many things. Most on the automatic mapping, which is working really good, but it can't guess your thoughts. This is why you should use "...For" methods, because they will generate field names using the same logic as the mapper uses. So check the field name: is the "H" at the beginning also in the model's property name? If not, remove it!

If you post the model, the view and the action (only the prototype, not the method body) you are posting to, I can tell you precisely what you are missing.
 
Share this answer
 
Comments
rajaupendrachowdary 26-Dec-12 7:09am    
Hi Frend,
@Html.Hidden("HProjectTypeId", Model.ProductTypeId); is the View
below is controller
[HttpPost]



#region old
public PartialViewResult ProjectAdd(FormCollection _collection)
{

var hiddenId = _collection["HProjectTypeId"];
}
but getting the null value even i was used HiddenFor.
Zoltán Zörgő 26-Dec-12 7:28am    
What on earth is FormCollection? I know what it is, but why are you using it? I don't want to link tutorials for you, but you should read some. Because you miss the point of MVC!
Here is the very basic "edit" scenario:
0) The "get" version of the edit action is called
1) You read from the data model store the entity based in it's key
2) You populate your view model for the edit view (most likely much alike the data model for the same entity, but not the same)
3) You pass the populated model to the View
4) The view is rendered with the data from the view model
5) The user edits the data and post the form
6) The "post" version of the edit action is located, this should expect an object as parameter that has the type of the view model you used - now, you don't follow this part
7) The mapper maps the post fields to the parameters, and the action is executed

Now, since you have not used this pattern, you have to deal with several things on your own. Since you posted really little of your code, I can only guide you:
A) If you have IE9, start the developer toolbar - if an earlier IE, install Fiddler and start it. Use these tools, to see how the http post request body is populated with the form fields, since these are mapped - or, in your case, these will be the keys in the _collection.
B) Do a breakpoint at the code row you posted, and inspect the _collection variable, to see what's in it.
C) Be sure, to have your hidden field inside the same form! Check the generated html.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900