Pass data from View to Controller in ASP.Net MVC

There are many ways to pass data from Controller to View – ViewBag,ViewData, TempData,though a Model.

But you wonder how you can pass data from View to Controller

Looking for quick answer?

If you came from Google and are looking for quick answer, the answer is this: you’ll have to submit the form (in the View) either through GET or POST method to pass data from rendered View to Controller. But I want you to be better developer. So, I strongly recommend you to read through the entire article to understand what’s happening behind the scenes.

Even though View is created at server side but it is rendered at the browser for the view. In the below picture, Internet explorer icon at the left hand side represent the browser and the right hand side represent the server. Please note that the View(UI) is created at the server side at step 5 but the actual form is rendered to the user at the browser at step 7.

When you request a new page (containing a form), following are the high level events happen

ASP.Net MVC Flow

 

 

  1. User request a web page containing a form
  2. Request reaches routing engine of ASP.Net MVC
  3. Based on URL pattern, routing engine selects a particular controller
  4. Controller creates empty model and pass it to the view
  5. View gets the model and creates a form using HTML helpers
  6. Controller returns the View
  7. Finally, the web server responds the generated HTML to the browser

So, if you want to pass the data from the rendered View (client side- browser) to the controller (server side), you’ll have to submit the form. Primarily, there are couple of ways to submit the form – GET and POST. In the below picture, see what happens when you submit the form.When user is trying to submit the form, below are the high level events happen

 

ASP.Net MVC Flow Form Submission

 

 

  1. User entered the information and submits the form
  2. Request reaches the Routing engine of ASP.Net MVC
  3. Based on the form post method and action , it selects the appropriate controller
  4. Controller gets the data and save it to the database
  5. A new page is created to inform the user that the entered informed is saved successfully
  6. Controller returns the View
  7. Web server sends that generated View to the browser

 

Example Code:

Let me explain you with the simple example code. Let us assume we have a simple form – with name and age fields. You want to pass this data from View to Controller. Let’s see how we can do this.

Create User.cs in Models folder – This user class is our model


public class User
{
  public string username { get; set; }
  public string age { get; set; }
}

We are passing the empty model to user – This model would be filled with user’s data when he submits the form.

public ActionResult AddWithHTMLHelperAndModel()
{
  var model = new User();
  return View(model);
}

This information is needed for strong typing in using model information.

@model TextInput.Models.User
@using (Html.BeginForm("AddWithHTMLHelperAndModel", "User", FormMethod.Post))
{
  @:UserName : @Html.TextBoxFor(m => m.username)
  @:Age : @Html.TextBoxFor(m => m.age)
  <input type="submit" value="Submit" />
}

By convention, all HTML helpers with associated models would suffix with ‘For’. Earlier we have used, @Html.TextBox. Now, we are using @Html.TextBoxFor when using with Model
[HttpPost]
public ActionResult AddWithHTMLHelperAndModel(User user)
{
  return Content("Received Username:" + user.username + " Age:"+user.age);
}

If you like this article, please subscribe. I’ll send you useful articles along the way.

Want actionable useful articles on .Net development?