Using Partial Class Instead Of Model class In ASP.NET MVC

Introduction

In this article we will learn how to use partial class instead of Model Class in MVC using entity framework

First understand what is partial class

A partial class is one that can be split among multiple physical files. This feature came in C# 2.0. The partial class break the definition of class two or more than two class files, but it will be together at compile time as one class.

Now here we will be using partial concept in MVC using entity framework

Database structure

Create a table in the database with the name TBurl.

The following is my table design.


Create MVC Application 

Step 1: Go to File, New, then Project.

Step 2: Go to File, New, Project. Choose "ASP.NET MVC 4 Web Application" from the list, then provide the application name as you wish and set the path in the location input where you want to create the application.
 
Step 3: Now choose the Project Template "Basic".

Adding a ADO.Net Entity Data Model

Step 1: 
Right-click on the project and select "Add new item", then select Data from the templates.
 
Step 2: Choose "ADO.Net Entity Data Model" from the list and provide a name. Now, after clicking on Add, you can see the .edmx file in the project.

Step 3:



Create Model Class

The MVC model contains all the application logic validation, business logic and data access logic. We can create a TBurl class under the Model Folder.

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.ComponentModel.DataAnnotations;  
  4. using System.Linq;  
  5. using System.Web;  
  6.   
  7. namespace urlhyperlink  
  8. {  
  9.     [MetadataType(typeof(urlmetadata))]  
  10.     public partial  class TBurl  
  11.     {  
  12.     }  
  13.     public class urlmetadata  
  14.     {  
  15.         public int URLid { getset; }  
  16.         [DataType(DataType.EmailAddress)]  
  17.         public string EmailAddress { getset; }  
  18.         [DataType(DataType.Url)]  
  19.         public string PersonalWebsite { getset; }  
  20.         public string FullName { getset; }  
  21.         public string Gender { getset; }  
  22.       
  23.     }  
  24. }  
In the above code I am created partial class  with name TBurl, and I have same class inside our sampleDtataModel that is created automatically if I am using entity Framework using wizard. In this class I am not adding model validation, since  in future if I want to override the table inside sampleDtataModel, all the validations in the class will lost. This is the reason we need to create partial class here. 



In above figure I have not added any model validation, I have used all the validations in partial class Tburi.

Create Homecontroller

  1. using System;    
  2. using System.Collections.Generic;    
  3. using System.Linq;    
  4. using System.Web;    
  5. using System.Web.Mvc;    
  6.     
  7. namespace urlhyperlink.Controllers    
  8. {    
  9.     public class HomeController : Controller    
  10.     {    
  11.     
  12.         public ActionResult Detail(int id)    
  13.         {    
  14.             informationEntities rf = new informationEntities();    
  15.     
  16.             TBurl tb = rf.TBurls.Single(p => p.URLid == id);    
  17.     
  18.            return View(tb);    
  19.     
  20.         }    
  21.     
  22.     }    
  23. }    
Create Detail View
  1. @model urlhyperlink.TBurl    
  2.     
  3. @{    
  4.     ViewBag.Title = "Detail";    
  5. }    
  6.     
  7. <h2>Detail</h2>    
  8.     
  9. <div style=" font-family:Arial">    
  10.     @Html.DisplayForModel();    
  11.     
  12. </div>   
Run your Application by pressing F5 key.

Output



Summary

In this article we learned about partial class in MVC using entity framework.


Similar Articles