Customizing Autogenerated View in Entity Framework in ASP.NET MVC

In my previous tutorial we learned CRUD operation using EntityFramework in ASP.NET MVC. When we work with Entity Framework it creates Views (For creation,insert,delete,update) automatically. So the output of the application is,



Changes in Index View

Suppose when we get output where Header name is common for two fields then we need to change their header name for recognition. Here if we change StudentName as “
Name” then StudentName and DepartmentName has same header name as “Name" then we need to customize in index view. These header names are the same as database table column name.
Means o/p is like below:



So here I want to change the Header to “student Name” instead of “Name”,

Step 1: How header generates.

When we create a application using entityFramework we add a EntityDataModal then we gives connection and select tables that time it creates a class in modal (Designer class).



When we go to this class we see that it’s a partial class, And this is an auto generated class. This contains all the properties (In region
#region Primitive Properties); These property names match with the Table column Name.

Step 2: How to change Header name.

We can change this header name either change column name in database table or change the properties name in Student class which is auto generated class.

If we change in auto generated student class is not good because due this we can loss all the changes , So we have to customization in different class.

This auto generated class is a partial class so we can create a another student partial class and we can make changes there.

So in modal folder add a class and give the name as “Student.cs” and write the following code in this class.
  1. [MetadataType(typeof(StudentMetaData))]  
  2. public partial class Student  
  3. {}  
  4. public class StudentMetaData  
  5. {  
  6.     [Display(Name = "Student Name")]  
  7.     public string Name  
  8.     {  
  9.         get;  
  10.         set;  
  11.     }  
  12. }  
This display attribute present in “"System.ComponentModel.DataAnnotations" namespace.

When we run this application it will generate a Header name As “StudentName” instead of Name.

Changes in Edit View

When we run this application and click on edit link then we see that “Gender” is not in dropdown and Department does not have default text.

Go to the create view and replace following code.
  1. @Html.DropDownList("DepartmentId", String.Empty)  
  2.   
  3. WITH  
  4. @Html.DropDownList("DepartmentId""Select Department")  
  5. For Gender  
  6. @Html.EditorFor(model => model.Gender)  
  7.   
  8. WITH  
  9. @Html.DropDownList("Gender"new List<SelectListItem>  
  10. {  
  11. new SelectListItem { Text = "Male", Value="Male" },  
  12. new SelectListItem { Text = "Female", Value="Female" }  
  13. }, "Select Gender")  
For making Name as read only make following changes,
  1. @Html.EditorFor(model => model.Name)  
  2.   
  3. TO  
  4. @Html.DisplayFor(model => model.Name)  
  5. @Html.HiddenFor(model => model.Name)  
Changes in create View

Here any field is not required in create, which means if we click on create button it saves NULL in database for all fields that is wrong, So for that we need to make require field using [Required] attribute that present in “using System.ComponentModel.DataAnnotations;” namespace.

So lets make changes in Student class which we create as partial class,
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5. using System.ComponentModel.DataAnnotations;  
  6. namespace CUROperationUsingEF.models  
  7. {  
  8.     [MetadataType(typeof(StudentMetaData))]  
  9.     public partial class Student  
  10.     {}  
  11.     public class StudentMetaData  
  12.     {  
  13.         [Required]  
  14.         public string Name  
  15.         {  
  16.             get;  
  17.             set;  
  18.         }  
  19.         [Required]  
  20.         public string Gender  
  21.         {  
  22.             get;  
  23.             set;  
  24.         }  
  25.         [Required]  
  26.         public string City  
  27.         {  
  28.             get;  
  29.             set;  
  30.         }  
  31.         [Required]  
  32.         [Display(Name = "Department")]  
  33.         public int DepartmentId  
  34.         {  
  35.             get;  
  36.             set;  
  37.         }  
  38.     }  
  39. }  
Replace the following code in Create view also,
  1. @Html.DropDownList("DepartmentId", String.Empty)  
  2.   
  3. WITH  
  4. @Html.DropDownList("DepartmentId""Select Department")  
  5. For Gender  
  6. @Html.EditorFor(model => model.Gender)  
  7.   
  8. WITH  
  9. @Html.DropDownList("Gender"new List<SelectListItem>  
  10. {  
  11.    new SelectListItem { Text = "Male", Value="Male" },  
  12.    new SelectListItem { Text = "Female", Value="Female" }  
  13. }, "Select Gender")  

Up Next
    Ebook Download
    View all
    Learn
    View all