Hello friends, In this article I will help you to understand the View Model binding and how to achieve it in ASP.NET MVC with C# code example. The project template which I will use here is ASP.NET core. I will demo by creating a new View and Model, as well as we see how the data is traversed between view and mode.
Starting the ASP.NET MVC Project
I have created a default ASP.NET MVC application as shown in the below figure.
And select Ok on the pop up. In the next pop up select the Web Application(Model – View – Controller) as shown in the figure below. There is no need to select Enable Docker Support checkbox and its optional to select the “Configure for HTTPS” checkbox.
Once you are done with the above pop up click OK and you will get the default template for ASP.NET core template.
If you run the application you will get a nice looking website as shown in the figure below.
I will change the “Contact Me” page to accept some parameter fields and bind it to a model.
View Model Binding in ASP.NET MVC
Change the contents of the Contact.cshtml as shown in the figure below. For the ease of finding the location I have the Solution explorer open as well.
The html contents as below. This can help you to copy paste the contents.
@{ ViewData["Title"] = "Contact"; }
As shown in the above HTML, I have kept the HTML controls in the form element and added the method attribute with POST.
When the button inside this form is clicked it will send a post request to the HomeController. We need to have a corresponding Contact method in the HomeController to handle the request. Though we already have one Contact method, but that one serves the GET request.
Add the below method to the HomeController.cs
[HttpPost] public IActionResult Contact(object contact) { return View(); }
Once you are done with your changes, run the project and open the website in the browser. Go to the “Contact Me” tab and fill the information as shown in the figure below. Finally click on the button “Message”.
And you keep a break point at the Contact method we added previously. Do a quick watch on the this.Request and you can see the parameters, I have added and their values.
This shows that we are getting the values we have added to the form. But we need to bind it to the Model.
Add the below class to the Models folder.
public class ContactViewModel { public string Name { get; set; } public string Email { get; set; } public string Subject { get; set; } public string Message { get; set; } }
And accept this class as the parameter to the contact post method we added previously. Once again fill the data and click the button and as you can see in the figure below, the values of the text fields are bind to the class properties.
This happened because of the name attributes we have provided for the html elements in the above html code. As you can see the name is same as the property of the our ContactViewModel class.
Model Binding in ASP.NET MVC
All this binding of the domain model types to the incoming post request happens due to the model binding feature of ASP.NET MVC. Model binding, a useful MVC feature whereby incoming data is parsed and the key/value
pairs in the HTTP request are used to populate properties of domain model types.
Model binding is a powerful and customizable feature that eliminates the grind of dealing with HTTP requests directly and lets you work with C# objects rather than dealing with individual data values sent by
the browser.
The ContactViewModel object that is passed as the parameter to the action method is automatically populated with the data from the form fields.
Model Binding using Tag Helpers
In the previous HTML I have used the traditional HTML attributes to bind the elements to my ContactViewModel class. We can use the asp-for attribute tag helper which simplifies the HTML. Below is the HTML code with the HTML tag helpers
@model ContactViewModel @{ ViewData["Title"] = "Contact"; }
Conclusion:
In this article I have shown you how easy it is to deal with the HTTP requests and HTML elements by using the view model binding feature of ASP.NET MVC. I have shown you how we can send back and forth the data between the view and the controller.
References:
Building a Web App with ASP.NET Core, MVC, Entity Framework Core, Bootstrap, and Angular
Leave a Reply