Table of contents
1.
Introduction
2.
Model Binding
3.
Model Binding Example
4.
FAQs
5.
Key takeaways
Last Updated: Mar 27, 2024

Web Forms Model Binding

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

Introduction

ASP.NET 4.5 brought out web forms that supported model binding to the view. This helped make data objects and sending them to the backend easier. Let us first see how to make a model. Then we will create a form in the frontend, along with functionality to add model binding of form model with the form in the view.

Model Binding

Model binding is the process of binding models between the controller and views. Changes to one of the controllers and view will reflect on the other instantly.

Let us look at the steps below:

  1. We create an ASP.NET web app

2. Then we will select the template web forms.

3. We create a new form with the help of master page template.In order to display model data, we’ll make use of it.

4. We then add a data model here.

Model Binding Example

The data coming from HTTP requests has been bound to the view here.

Model binding here is used by calling the data for a particular id in the query. ASP.NET will take out the id field and provide it as an argument to the action method. 

app.UseEndpoints(endpoints =>
{
    endpoints.MapControllerRoute(
        name: "default",
        pattern: "{controller=Home}/{action=Index}/{id?}");
});
public Action Index(int id)
{
    return View(repository[id]);
}

 

More illustration: 

User.cs

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Web; 
namespace Temp.Models 
{ 
   public class Student 
   { 
       public int _id { get; set; } 
       public string _name { get; set; } 
       public string _email { get; set; } 
       public string _contact { get; set; } 
   } 
} 

 

Above is a model that includes student data containing unique IDs, name, email and contact. let us now create a controller.

User.aspx.cs

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

 

After this, we need to create a view and add the model, name of the view to it, which will create a view for us as below.

Output:

FAQs

  1. What are web forms?
    A web form is an HTML form that makes the user input the data to be able to send it through the backend. The web forms have enabled user interactions with DOM easily. It is on the view layer of MVC architecture.
     
  2. What is model binding?
    This term model binding refers to synchronized changes to be reflected in both the view and the database. In the same way, models can be bound with data objects coming from the backend.
     
  3. How do you manipulate the view using model binding?
    Binding form elements to the model then reflecting those changes upon interaction with the DOM is the idea behind manipulating views. Functionalities in the controller can be used to manipulate the DOM. 
     
  4. What is the MVC pattern in ASP.NET?
    It implies a Model View Controller pattern. This is focused on separating logic to build a web application. The controller holds on to the view; the view displays the content, interaction through the view will lead to change in the database, which again triggers the controller to reflect changes on to the view. This pattern has been popular and used by many web dev frameworks.
     
  5. What is a master page?
    Master pages give consistent look and behaviour to all pages in an ASP.NET application. It provides templates, placeholders for the content of all the pages that have a shared layout and common functionality. We can customise and write our own logic in them.

Key takeaways

Congratulations on getting through this article. This article showed how web forms are used with model binding in the ASP.NET framework.

You can also explore many other open-source, server-side web-application frameworks other than ASP.net with the article Top 10 web development frameworks.

Happy Learning!

Live masterclass