[ASP.NET MVC ] How to Receive Data From Request to a Controller

December 31, 2011 at 10:26 pm Leave a comment

A Controller is where you are interpreting a request and create appropriate model objects. You action method can get data from a “GET” request or a “POST” request. In MVC, some data can be passed as route data values.

Let’s find out how you can access all of these data in a controller.

1. Receiving Data From Request to Controller

Controllers are getting request data in many forms: query string values, form values, and route parameter values.

There are 3 ways to access incoming data:

  • Context Objects: HttpContext, Request, Response, RouteData, etc …
  • Action Method Parameters¬†
  • Data Model Binding

2. Context objects

The “System.Web.Mvc.ControllerBase” class provides the “ControllerContext” property to access various context objects. But the “Controller” class provides the direct access properties (convenience properties) to these context objects:

  • public HttpContextBase HttpContext { get; }
  • public HttpRequestBase Request { get; }
  • public HttpResponseBase Response { get; }
  • public RouteData RouteData { get; }

Among these, we are mostly interested in “Request” and “RouteData” properties.

You can access the following data from the “Request” object:

  • public virtual NameValueCollection QueryString { get; } // GET variables
  • public NameValueCollection Form { get; } // POST variables
  • public HttpCookieCollection Cookies { get; }
  • public NameValueCollection Headers { get; }
  • public string HttpMethod { get; }

The “System.Web.Routing.RouteData” class provides the “Values” property to access route parameter values.

  • public RouteValueDictionary Values { get; }¬† // route parameters
@using (Html.BeginForm("LoginResult", "Account"))
{
  <p>Name: <input type="text" name="name" /></p>
  <p>Password: <input type="password" name="password" /></p>
  <input type="submit" value="Login" />
}
[HttpPost]
public ActionResult LoginResult()
{
  string name = Request.Form["name"];
  string password = Request.Form["password"];
  if (name=="homer" && password=="donuts")
    ViewBag.Message = "Succeeded";
  else
    ViewBag.Message = "Failed";
  return View();
}

3. Action Method Parameters

Accessing data using the “Request.Form” or “Request.QueryString” is not ideal because you need to type the names manually, which are error-prone.

The better way is to use the parameters of an action method. If the name of the parameter matches with the key name of one of following collections, data are passed automatically.

  • Request.Form
  • Request.QueryString
  • Request.Files
  • RouteData.Values
[HttpPost]
public ActionResult LoginResult(string name, string password)
{
  if (name=="homer" && password=="donuts")
    ViewBag.Message = "Succeeded";
  else
    ViewBag.Message = "Failed";
  return View();
}

4. Data Model Binding

When you are working with a strongly-typed views, you will have a better option to access form data. MVC creates an object for you.

namespace Mvc3App.Models
{
  public class Account
  {
    public string Name { get; set; }
    public string Password { get; set; }
  }
}
@model Mvc3App.Models.Account
@{ ViewBag.Title = "Login"; }
<h2>Login</h2>
@using (Html.BeginForm())
{
  <p>Name: @Html.TextBoxFor(m => m.Name)</p>
  <p>Password: @Html.PasswordFor(m => m.Password)</p>
  <input type="submit" value="Login" />
}
public class AccountController : Controller
{
  public ActionResult Login()
  {
    Account acc = new Account { Name = "homer" };
    return View(acc);
  }
  [HttpPost]
  public ActionResult Login(Account acc)
  {
    if (acc .Name== "homer" && acc.Password == "donuts")
      ViewBag.Message = "Succeeded";
    else
      ViewBag.Message = "Failed";
    return View("LoginResult");
  }
}
About these ads

Entry filed under: ASP.NET, MVC. Tags: , .

[ASP.NET MVC ] How to Pass Data From a Controller to a View [ASP.NET MVC] Views – ASPX View (WebFormViewEngine)

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

Trackback this post  |  Subscribe to the comments via RSS Feed


About

As a web developer, I am happy to have a place to share my little know-hows regarding web-development.

MCSD

Recent Posts

Categories


Follow

Get every new post delivered to your Inbox.

%d bloggers like this: