part 27- Customizing the autogenerated index view

使用Entity Framework的Database First自动生成扩展名为edmx的文件,然后又通过“包含视图的 MVC 控制器(使用Entity Framework)”的方法自动生成控制器和视图后。

In this video 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 asDepartment 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" andDepartmentMetaData 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
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值