Simon Rigby
Simon Rigby

Reputation: 1786

Get Form Fields and Model Data in a Controller Action MVC3

I have a situation where I have a strongly typed View which is passed a ViewModel from a controller action. When the user submits the form in the view back to the controller I can get access to the values that have been set in the form via the properties of the View Model (all good so far). If my form contains other controls that are not bound via the view model can I quiz those values after the post. For example if I had a bunch of text boxes that are bound to strings in my View Model and a checkbox that doesn't form part of the data on that View Model can I get access to both on post back.

Hope that makes sense.

Cheers

Simon.

Upvotes: 3

Views: 6301

Answers (1)

Nitin Varpe
Nitin Varpe

Reputation: 10694

You can access form fields by its name from view to controller

In View

<input type="text" name="fname"/>

In Controller

public ActionResult YourAction(Model model,string fname)
{
  //Access fname here
}

If your want to access multiple values from view which are not binded to model you can use FormCollection

public ActionResult YourAction(Model model,FormCollection form)
    {
      //Access fname here like below
      var firstName=form["fname"];
    }

Upvotes: 6

Related Questions