views:

51

answers:

2

can we access the hidden variables values in a controller's ActionResult if the hidden variable's value is set in javascript?

A: 

If you are referring to ASP.NET MVC, and the hidden variable that you mention is actually a hidden field in a form that is being posted to the controller, then the answer is yes.

This is a frequent pattern in the apps that I write. Say, for example, that you are editing the details of a person. The form that you fill in would contain visible fields for things like name, age, etc - but would also need to have a hidden filed that holds the ID of the person that you are editing the details for.

If this is the kind of scenario that you are using, then the hidden field is available to the controller in the same way as the name & age fields.

EDIT: Further to your subsequent comment, it looks like you are referring to javascript variables. If this is the case, then these are not available to the controller directly - but it is possible to arrange this by inserting the variable(s) into the form.

//Javaacript
var myVariable = calculateSomeValue();
$("#myFormField").val(myVariable);
...
//HTML
<form action="..." method="post">
    <input type="hidden" name="myFormField" id="myFormField"/>
    ...
</form>
...
//Controller code
ActionResult MyControllerAction(string myFormField, ...){
    DoSomethingWith(myFormField);
}

If this doesn't help, can you post some example code?

belugabob
actually i will be setting some calculated values to the hidden fields. i want these values to be accessible in the ActionResult of a controller. is this possible? could you please share more info / links on this...
vinay
My post pretty much sums it up - calculate some values, stuff them into hidden fields and define those fields as arguments for your Action method. Don't know how much simpler it could be?
belugabob
belugabob.. thnx for your quick responses. the constraint that i am having is that there will be more than 25 params that needs to be accessed in order to generate a report in pdf. in that case if all these values are passed as arguments then the url will not be seo friendly. for that reason i was thinking of setting these values in hidden variables and access them in the controller, as we do in our conventional asp web applns.. if you have any other better approach to achieve this, plz suggest.
vinay
Would you care to explain how your conventional ASP web app acheives your goal, and why you can't acheive the same in MVC? The only way to pass information for a clinet side variable to a server side Action method is via the URL or a form POST (And the later would definitely not be SEO friendly.
belugabob
A: 

When you post data to a Controller, the entire form's contents (including hidden fields) can be posted. The purpose of a hidden field is to hide it in the client, not when it's data is sent to the server. Form data is packaged up in a POST and sent to the server, regardless of how it is populated, so you can use JQuery to populate the fields just fine...

Given this form:

<% Html.BeginForm(); %>
  <input type="hidden" id="catID" name="catID" />
<% Html.EndForm(); %>

You could process it as a route parameter:

[AcceptVerbs(HttpVerbs.Post)]
public ActionResult DoSomething(string catID)
{
  // Do stuff here...
}

Or, as an item of the FormCollection instance:

[AcceptVerbs(HttpVerbs.Post)]
public ActionResult DoSomething(FormCollection form)
{
  string catID = form["catID"];

  // Do stuff here...
}

Or even as an input model:

public class MyInputModel
{
  public string catID;
}

[AcceptVerbs(HttpVerbs.Post)]
public ActionResult DoSomething(MyInputModel input)
{
  string catID = input.catID;

  // Do stuff here...
}
Matthew Abbott
i will be setting values to an hidden variable like this $('#catID').val('asd') in a client function() which will be called from an HTML.ActionLink. Now can the value of "catID" be accessed in the ActionResult? if yes, could you please give some syntax / code to do this? Thnx..
vinay
Example updated :)
Matthew Abbott