Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 714
Simon Rigby Avatar asked Nov 22 '25 18:11

Simon Rigby


1 Answers

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"];
    }
like image 151
Nitin Varpe Avatar answered Nov 24 '25 23:11

Nitin Varpe



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!