RSS

Get Form’s Post Values in ASP.NET MVC with FormCollection

20 Mar

In addition to this post, you can also get the form’s post values with System.Web.Mvc.FormCollection in ASP.NET MVC 2 or MVC 3.

Here are my controllers, one for display the page, one for process page post back:

public ActionResult SubmitFormWithFormCollection()
{
    return View();
}
[HttpPost]
public ActionResult SubmitFormWithFormCollection(FormCollection formCollection)
{
    foreach (string _formData in formCollection)
    {
        ViewData[_formData] = formCollection[_formData];
    }

    return View();
}

Here’s my code in Razor view engine:

@{
    ViewBag.Title = "Submit Form With FormCollection";
}

<h2>Submit Form with FormCollection</h2>

@using (Html.BeginForm())
{
    <div>
        You have submitted:<br />
        Firstname: @ViewData["firstname"]<br />
        Lastname: @ViewData["lastname"]<br />
        Email: @ViewData["email"]<br />
    </div>
    <br />

    <label for="firstname">Firstname:</label>
    <input type="text" name="firstname" />
    <br />
    <label for="lastname">Lastname:</label>
    <input type="text" name="lastname" />
    <br />
    <label for="email">Email:</label>
    <input type="text" name="email" />
    <br />
    <br />

    <input type="submit" name="submit" />
}

When the view is loaded, it looks like:

And when it posted:

FormCollection (System.Web.Mvc.FormCollection) class contains key / value pairs from all the fields in the form submitted.

The FormCollection’s key is the ‘name’ attribute of the HTML control field in the form while the value is the user’s input to that field. So, to get value of the ‘firstname’ field, I can do:

[HttpPost]
public ActionResult SubmitFormWithFormCollection(FormCollection formCollection)
{
    ViewData["firstname"] = formCollection["fistname"];

    return View();
}

There are many ways to iterate through System.Web.Mvc.FormCollection. See this post for detail.

About these ads
 
14 Comments

Posted by on March 20, 2011 in General

 

Tags: , ,

14 responses to “Get Form’s Post Values in ASP.NET MVC with FormCollection

  1. ajit

    November 9, 2011 at 12:46 am

    i want to know how can i get values of the html form in asp.net

     
  2. Chris

    June 21, 2012 at 3:42 am

    Thanks, helped clear a lot up

     
  3. Awais

    September 24, 2012 at 8:52 am

    It really helped me. Thanks

     
  4. Raj

    January 3, 2013 at 10:42 pm

    It really helped me tooo :)

     
  5. Dima

    February 4, 2013 at 6:08 am

    somthing good))

     
  6. shanky

    May 1, 2013 at 11:32 pm

    simple and effective

     
  7. amit

    May 8, 2013 at 12:48 am

    Thank’s

     
  8. jean neir

    June 6, 2013 at 4:32 am

    thanks, worked well.

     
  9. Postman

    June 19, 2013 at 2:26 am

    Thanks, it works.

     
    • dam

      March 22, 2014 at 10:37 pm

      It worked for me also..nice article..

       
  10. Jija

    August 26, 2013 at 3:22 am

    Hi, This is one is good and work also. Thanks for that. but I want to pass one form value to another form.

     
  11. dam

    March 22, 2014 at 10:38 pm

    nice article

     

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
Follow

Get every new post delivered to your Inbox.

Join 49 other followers

%d bloggers like this: