I have several views that have a hidden field like this:
<input type="hidden" runat="server" id="hiddenTab" value="1"/>
each view may have a different value.
I have a base controller that I want to be able to access this value on the constructor:
public class BaseController: Controller
{
public BaseController()
{
string tabid = Request.Form["hiddenTab"];
}
}
and finally I have several controllers that implement this base controller:
public class HomeController: BaseController
{
public ActionResult Index()
{
string tabid = Request.Form["hiddenTab"];
//if tabid = 1, do this.. if it is 2, do this, etc...
}
}
How can I access the hiddenfield value or the value of any other control on the view for that matter when the controller is first loading? The application is just starting at this point, so nothing has been posted yet. I have tried to access the value from both the controller and the base controller and the Request.Form has no values in it.
I am converting an application from aspx to MVC and on the aspx app, the masterpage codebehind had this:
HtmlInputHidden hidddentabid;
hidddentabid = (HtmlInputHidden)Body.FindControl("hiddenTab");
This worked fine for a master page in aspx, but not in MVC. I have spent hours looking and can't find anything on this. All I am able to find is how to access hidden values that are POSTED to a controller. Nothing on when a controller for a view is first rendered.
Any help would be greatly appreciated!
you are missing the name
property for the hidden element. you can get the values based on the name. try this
<input type="hidden" id="hiddenTab" name="hiddenTab" value="1"/>
as Andrie mentioned runat="server"
is not required in Asp.Net MVC