Model Binding in ASP.Net 4.5

Introduction

We really like using the model-binding feature of client-side validation. The first thing we do is to apply model binding to automate the process of getting form values and using them to populate the properties of the Person object. The details of the form data are displayed using the p element in the markup.

Create a new project using "File" -> "New" -> "Project..." then select Web then select "ASP.Net Web Forms Application". Name it “ModelBinding".

web application library

For the new ASP.NET Project select Empty template then select the Web Forms checkbox then click OK.

empty template

Now, in the project in the Solution Explorerr click on Models then select Add > Class.

adding new class

Now add a new item then select class > Contact.cs then click Add.

class libray file

Now to apply Models attributes to the Contact class.

Contact.cs

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5.   
  6. namespace ModelBinding.Models  
  7. {  
  8.     public class Contact  
  9.     {  
  10.         public string FirstName { getset; }  
  11.         public string LastName { getset; }  
  12.         public string Email { getset; }  
  13.         public string Phone { getset; }  
  14.         public DateTime DateOfBirth { getset; }  
  15.         public string State { getset; }  
  16.     }  
  17. }  
Now add a new item then select Web Form and leave Default.aspx as the name then click Add.

webforms and style sheet

The details of the form data are displayed using the p element in the markup.

The following are the contents of the Default.aspx:
  1. <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="ModelBinding.Default" %>  
  2.   
  3. <!DOCTYPE html>  
  4.   
  5. <html xmlns="http://www.w3.org/1999/xhtml">  
  6. <head runat="server">  
  7.     <title>Model-binding</title>  
  8.     <link href="Style.css" rel="stylesheet" />  
  9. </head>  
  10. <body>  
  11.     <form id="form1" runat="server">  
  12.         <h3>Contact Formdata Model Binding in ASP.NET 4.5</h3>  
  13.         <div>  
  14.   
  15.             <label>First Name:</label><asp:TextBox ID="FirstName" runat="server"></asp:TextBox>  
  16.         </div>  
  17.         <div>  
  18.             <label>Last Name:</label><asp:TextBox ID="LastName" runat="server"></asp:TextBox>  
  19.         </div>  
  20.   
  21.         <div>  
  22.             <label>Email:</label><asp:TextBox ID="Email" runat="server"></asp:TextBox>  
  23.         </div>  
  24.   
  25.         <div>  
  26.             <label>Phone:</label><asp:TextBox ID="Phone" runat="server"></asp:TextBox>  
  27.         </div>  
  28.   
  29.         <div>  
  30.             <label>Date Of Birth:</label><asp:TextBox ID="DateOfBirth" runat="server" TextMode="Date"></asp:TextBox>  
  31.         </div>  
  32.   
  33.         <div>  
  34.             <label>State:</label><asp:TextBox ID="State" runat="server"></asp:TextBox></div>  
  35.        <div>  
  36.         <label></label><asp:Button ID="btnSubmit" runat="server" Text="Submit" OnClick="btnSubmit_Click" />  
  37.            </div>  
  38.         <p id="DataText" runat="server"></p>  
  39.     </form>  
  40. </body>  
  41. </html>  
Next, create the code-behind as follows: Default.aspx.cs
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5. using System.Web.UI;  
  6. using System.Web.UI.WebControls;  
  7.   
  8. //using Namespace & Models  
  9. using System.Web.ModelBinding;  
  10. using ModelBinding.Models;  
  11.   
  12. namespace ModelBinding  
  13. {  
  14.     public partial class Default : System.Web.UI.Page  
  15.     {  
  16.         protected void Page_Load(object sender, EventArgs e)  
  17.         {  
  18.              
  19.         }  
  20.   
  21.         public void _binddataObject()  
  22.         {  
  23.             Contact dataObject = new Contact();  
  24.             if (TryUpdateModel(dataObject, new FormValueProvider(ModelBindingExecutionContext)))  
  25.             {                 
  26.                 DataText.InnerText = String.Format("FirstName: {0},    LastName: {1},   Email: {2},  Phone: {3},  DateOfBirth: {4},    State: {5}",  
  27.                   dataObject.FirstName, dataObject.LastName, dataObject.Email, dataObject.Phone, dataObject.DateOfBirth, dataObject.State);  
  28.             }  
  29.             //else  
  30.             //{  
  31.             //    ShowMessage(dataObject.ToString());  
  32.             //}  
  33.         }  
  34.   
  35.   
  36.         /// <summary>  
  37.         /// This Click Event function is used for _binddataObject  
  38.         /// </summary>  
  39.         /// <param name="sender"></param>  
  40.         /// <param name="e"></param>  
  41.         protected void btnSubmit_Click(object sender, EventArgs e)  
  42.         {  
  43.             _binddataObject();  
  44.         }  
  45.         /// <summary>  
  46.         /// This function is used for show message.  
  47.         /// </summary>  
  48.         /// <param name="msg"></param>  
  49.         void ShowMessage(string msg)  
  50.         {  
  51.             ClientScript.RegisterStartupScript(Page.GetType(), "validation""<script language='javascript'>alert('" + msg + "');</script>");  
  52.         }  
  53.     }  
  54. }  
Now run the page, it will look like this: Default.aspx

output webpage

Now Info Fill this from.

filled output

Now click the Submit button, it will display the data binding HTML p element.

submit clicked

I hope this article is useful. If you have any other questions then please provide your comments in the following.


Similar Articles