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
- User request a web page containing a form
- Request reaches routing engine of ASP.Net MVC
- Based on URL pattern, routing engine selects a particular controller
- Controller creates empty model and pass it to the view
- View gets the model and creates a form using HTML helpers
- Controller returns the View
- 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
- User entered the information and submits the form
- Request reaches the Routing engine of ASP.Net MVC
- Based on the form post method and action , it selects the appropriate controller
- Controller gets the data and save it to the database
- A new page is created to inform the user that the entered informed is saved successfully
- Controller returns the View
- 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.
[x_subscribe form=”277″]