can we access the hidden variables values in a controller's ActionResult if the hidden variable's value is set in javascript?
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?
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...
}