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
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 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.
- using System;
- using System.Collections.Generic;
- using System.ComponentModel.DataAnnotations;
- using System.Linq;
- using System.Web;
- namespace urlhyperlink
- {
- [MetadataType(typeof(urlmetadata))]
- public partial class TBurl
- {
- }
- public class urlmetadata
- {
- public int URLid { get; set; }
- [DataType(DataType.EmailAddress)]
- public string EmailAddress { get; set; }
- [DataType(DataType.Url)]
- public string PersonalWebsite { get; set; }
- public string FullName { get; set; }
- public string Gender { get; set; }
- }
- }

In above figure I have not added any model validation, I have used all the validations in partial class Tburi.
Create Homecontroller
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- using System.Web.Mvc;
- namespace urlhyperlink.Controllers
- {
- public class HomeController : Controller
- {
- public ActionResult Detail(int id)
- {
- informationEntities rf = new informationEntities();
- TBurl tb = rf.TBurls.Single(p => p.URLid == id);
- return View(tb);
- }
- }
- }
- @model urlhyperlink.TBurl
- @{
- ViewBag.Title = "Detail";
- }
- <h2>Detail</h2>
- <div style=" font-family:Arial">
- @Html.DisplayForModel();
- </div>
Output

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

heszarPosted Aug 22, 2017, 4:24 AM
Thank you for your sharing, but what if I want more control over my inputs? For example, lets say that I want to use RegEx to validate Email field, therefore I need to put some logic in EamilAddress property, how could I achieve this?
Santhakumar MunuswamyPosted Oct 18, 2015, 3:52 AM
Good one
Shridhar SharmaPosted Oct 12, 2015, 2:03 PM
good work Atul
Mohammed IbrahimPosted Oct 12, 2015, 12:11 PM
nice
RakeshPosted Oct 12, 2015, 11:43 AM
Good share sir
Sibeesh VenuPosted Oct 12, 2015, 3:13 AM
Nice Share
Muhammad Aqib ShehzadPosted Oct 12, 2015, 3:08 AM
good article
Mukesh KumarPosted Oct 12, 2015, 3:03 AM
Great work
Nilesh JadavPosted Oct 12, 2015, 2:58 AM
Nice one sir