Reputation: 7375
I need to pass some hidden field from a view to a controller.
In index.chtml
<div id="root">
................
@Html.Hidden("HProjectTypeId", "somevalue")
</div>
The above is not placed in any form like Html.BeginForm
or ajax form. I need to get this value in action when loading itself.
If it is placed in a form it means we can get it easily from the FormCollection
or Request.Form["key"]
, but it is not placed in a form.
public ActionResult Index()
{
// here I need to get the hidden field "HProjectTypeId"
}
I am expecting some code in JQuery or JavaScript to do this.
thanks,
siva
Upvotes: 0
Views: 1659
Reputation: 7135
The situation you're in isn't entirely clear (e.g. why isn't the hidden field within an Html.BeginForm
?), but here's some suggestions:
Assuming the value you're writing into the hidden field comes from the server in the first place, how about not writing it into a hidden field at all, and using the TempData
collection instead? The originating action sets the value, and it'll be persisted in session until the next request, making it available to the next action which is called
...or:
Assuming a form submission causes the Index action to be executed (as you are "making a full post"), you could use JavaScript to copy the hidden field's value into another hidden field which is included in an Html.BeginForm
...or:
Similarly, you could use JavaScript to write the hidden field's value into a cookie, and use a custom ValueProvider for cookies to access it in your action.
Upvotes: 1