HI WELCOME TO SIRIS

Part 26 - Customizing the autogenerated index view

Leave a Comment
we will discuss, customizing the auto-generated index view. 

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.

0 comments:

Post a Comment

Note: only a member of this blog may post a comment.