Support us .Net Basics C# SQL ASP.NET ADO.NET MVC Slides C# Programs Subscribe Buy DVD

Part 26 - Customizing the autogenerated index view

Suggested Videos 
Part 23 - Why deleting database records using get request is bad
Part 24 - Deleting database records using post request in mvc
Part 25 - Insert update delete in mvc using entity framework



In this video we will discuss, customizing the auto-generated index view. Please watch Part 25, before proceeding. 

At the moment, the "Index" view is using "Name" as the column header for both employee name and department name. This is because "Name" column is used in both the database tables(tblEmployee & tblDepartment) and entity framework has used these column names to generate "Name" property in Employee and Department classes that are auto-generated. 

I want to change the depratment column header to "Department Name" instead of just "Name". To achieve this, add a class file with "name=Department.cs" to "Models" folder.



Copy and paste the following code in "Department.cs" file
[MetadataType(typeof(DepartmentMetaData))]
public partial class Department
{
}

public class DepartmentMetaData
{
    [Display(Name="Department Name")]
    public string Name { get; set; }
}

With these changes run the application and notice the column name is displayed as Department Name. This is achieved by using "Display" attribute that is present in "System.ComponentModel.DataAnnotations" namespace. 

If you are wondering why can't we apply "Display" attribute directly to the auto-generated "Department" class instead of creating another partial "Department" and DepartmentMetaData class. We can do it. There is nothing stopping us from doing it, but every time the Department class is auto-generated, our custom changes will be lost. This is the reason for creating another partial class, and applying our changes.

10 comments:

  1. Hello,
    I am using VS 2012 and I cannot create the new Department.cs file because an auto-generated one already exists. I can drill down into the EmployeeDataModel.edmx then EmployeeDataModel.tt and a Department.cs file is there. So now I am stuck and cannot progress further. How can I add the new model with the existing auto=generated model already there?

    ReplyDelete
    Replies
    1. you can create departments.cs file and rename the actual file to department.cs that way it works perfectly
      Remember the file is saved as Departments.cs
      [MetadataType(typeof(DepartmentMetaData))]
      public partial class Department
      {

      }
      public class DepartmentMetaData
      {
      [Display(Name = "Department Name")]
      public string Name { get; set; }
      }

      Delete
    2. is there a need to create another model file when using entity framework

      Delete
    3. You Need Not To Add Another Model Folder.Just Add A Another Class With Departments Name And Then In That Name Of Class Should Be Like

      Public Partial class Department
      {

      }

      and The Rest Of The Code

      Delete
    4. Check the Namespace. The Namespace of Department.cs & EmployeeDataModel.Designer.cs should be same.

      Delete
  2. what adventage from this way vs use viewModel ??

    ReplyDelete
  3. https://plus.google.com/u/0/106860210413123812771/posts/JdppZ7X2pHs?pid=6031471224056657410&oid=106860210413123812771

    How can I add the new model with the existing auto=generated model already there?

    ReplyDelete
  4. Hi Pragim,

    I have added Department.cs with Department as a partial class file for customization of header.
    I have checked for signature of name property in the department class which is automatically generated class and partial class which is explicitly created.

    Kindly provide the code for this part , it will be helpful for me

    Thanks in advance

    ReplyDelete
  5. Actually It was issue with the scope of the class. When I rechecked the Department class I have created was in Model folder and the AutoGenerated (.edmx)class was in Application root folder which was causing the issue.

    Now I kept both the classes in the same folder (Model) and I am able to see expected output.

    ReplyDelete
  6. How to Create MetadataType for WCF service? I am consuming WCF service, I want to customize the attributes of WCF response object in MVC. How to do that? CAn you please help me.

    ReplyDelete

If you like this website, please share with your friends on facebook and Google+ and recommend us on google using the g+1 button on the top right hand corner.