Support us .Net Basics C# SQL ASP.NET Aarvi MVC Slides C# Programs Subscribe Download

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.

16 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
  7. using this can i change the name of any column as different from the table present in SQL server.

    ReplyDelete
  8. I am not able to see the designer.cs file.
    When I am trying to change
    Code Generation Strategy =T4 to Legacy ObjectContext...I am getting 108 errors.

    ReplyDelete
  9. i am working in visual studio 2017 and i am unable to generate the department.cs class within the model folder. it says the class already exists. and if i try to name it with either upper or lower case it gives the same error

    ReplyDelete
  10. i am working in visual studio 2017 but unable to create Department.cs because compiler response you already exist Deparment.cs ...i created Deparments.cs then renam to try, visual studio again response you already exist Deparment.cs in this model folder please help me to solve this problem

    ReplyDelete
  11. i have created departments.cs file in model folder and done all are as mentioned above, i am using VS-2015. no error but column name are not changed in view

    ReplyDelete
  12. yes for department.cs problems class name departments.cs and after creation rename it to Department in coding area

    ReplyDelete

It would be great if you can help share these free resources