Table of contents
1.
Introduction
2.
Understanding Model Binding
2.1.
Use of Model Binding
3.
Example of Model Binding
3.1.
Create a Model
3.2.
Create a Controller
3.3.
Creating a view
4.
Frequently Asked Questions
5.
Key Takeaways
Last Updated: Mar 27, 2024

ASP.NET MVC Model Binding

Author Naman Kukreja
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

The programmers new to ASP.NET MVC found difficulty managing the View to the Model class values when they reached the action controller. So how to do this conversion?

The answer is by using Model Binder. It can adequately take care of the request from the View. We will learn more about Model Binding further in the blog, so let's move to our topic without wasting any time further.

Understanding Model Binding

The process of binding a model to view and the controller is known as Model Binding.

It is a well-designed bridge between C# action methods and HTTP requests. This will solve the problem we have discussed above, and because of this, it makes it easy for the programmers to work with view values.

It allows users to map a model with HTTP request data. Generally speaking, it is a process of creating a .NET object by using the data in the HTTP request sent by the user.

Use of Model Binding

Programmers use Model Binding to get the data submitted on the browser to the action controller. 

It automatically transfers the GET and POST requests into the user-specified data model.

ASP.NET uses default binders to do all these processes behind the scene.

Example of Model Binding

Here, we will create a simple model binding with controller and View. Here we are creating a Student model with some properties, and we will use those properties to create form fields.

Create a Model

To create a Model, right-click on the Model folder and add class. Now edit the default code with the below-given code:

using System;  
using System.Collections.Generic;  
using System.Linq;  
using System.Web;  
namespace MvcApplicationDemo.Models  
{  
    public class Student  
    {  
        public int ID { get; set; }  
        public string Name { get; set; }  
       
        public string Contact { get; set; }  
        public string Email { get; set; }  
    }  
}  

Create a Controller

After creating the model, we will now create a controller by Right-clicking on the controller folder and adding the class as shown below:

Now add the following code in it:

using System;  
using System.Collections.Generic;  
using System.Linq;  
using System.Web;  
using System.Web.Mvc;  
namespace MvcApplicationDemo.Controllers  
{  
    public class StudentsController : Controller  
    {  
        // GET: Students  
        public ActionResult Index()  
        {  
            return View();  
        }  
    }  
}

Creating a view

To create View, right-click on the index action method, and by selecting add view option, add the View and the corresponding model to the file.

The default file will contain some predefined code change that code with the following code:

@model MvcApplicationDemo.Models.Student  
@{  
    ViewBag.Title = "Index";  
}  
<h2>Index</h2>  
@using (Html.BeginForm())  
{  
    @Html.AntiForgeryToken()  
    <div class="form-horizontal">  
        <h4>Student</h4>  
        <hr />  
        @Html.ValidationSummary(true, "", new { @class = "text-danger" })  
        <div class="form-group">  
            @Html.LabelFor(model => model.Name, htmlAttributes: new { @class = "control-label col-md-2" })  
            <div class="col-md-10">  
                @Html.EditorFor(model => model.Name, new { htmlAttributes = new { @class = "form-control" } })  
                @Html.ValidationMessageFor(model => model.Name, "", new { @class = "text-danger" })  
            </div>  
        </div>  
 <div class="form-group">  
            @Html.LabelFor(model => model.Contact, htmlAttributes: new { @class = "control-label col-md-2" })  
            <div class="col-md-10">  
                @Html.EditorFor(model => model.Contact, new { htmlAttributes = new { @class = "form-control" } })  
                @Html.ValidationMessageFor(model => model.Contact, "", new { @class = "text-danger" })  
            </div>  
        </div>  

 
        <div class="form-group">  
            @Html.LabelFor(model => model.Email, htmlAttributes: new { @class = "control-label col-md-2" })  
            <div class="col-md-10">  
                @Html.EditorFor(model => model.Email, new { htmlAttributes = new { @class = "form-control" } })  
                @Html.ValidationMessageFor(model => model.Email, "", new { @class = "text-danger" })  
            </div>  
        </div>  
               <div class="form-group">  
            <div class="col-md-offset-2 col-md-10">  
                <input type="submit" value="Create" class="btn btn-default" />  
            </div>  
        </div>  
    </div>  
}  
<div>  
    @Html.ActionLink("Back to List", "Index")  
</div> 

 

Output

Upon executing, the index file produces the following output:

Frequently Asked Questions

1. What are some main advantages of using ASP.NET?

Answer: There are mainly three advantages of using ASP.NET: cross-platform High performance and open source.

2. What is HTTP protocol?

Answer: HTTP protocol refers to Hypertext Transfer Protocol.

3. Explain any use of HTTP protocol.

Answer: HTTP protocol is used as an application layer protocol for transferring files like HTML.

4. What do you mean by MVC Pattern?

Answer: It is referred to as Model, View, Controller. All these have different functions, and together they create a proper functioning project.

Key Takeaways

In this blog, we have learned about Model Binding in ASP.NET its use with good examples by creating MVC structure and the corresponding output.

Refer to this blog to learn about MVC Validations in ASP.NET. You will have a complete idea about MVC validations with basic annotations and all these with suitable examples.

Recommended Reading: 

Live masterclass