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.
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.
Hello,
ReplyDeleteI 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?
you can create departments.cs file and rename the actual file to department.cs that way it works perfectly
DeleteRemember 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; }
}
is there a need to create another model file when using entity framework
DeleteYou 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
DeletePublic Partial class Department
{
}
and The Rest Of The Code
Check the Namespace. The Namespace of Department.cs & EmployeeDataModel.Designer.cs should be same.
Deletewhat adventage from this way vs use viewModel ??
ReplyDeletehttps://plus.google.com/u/0/106860210413123812771/posts/JdppZ7X2pHs?pid=6031471224056657410&oid=106860210413123812771
ReplyDeleteHow can I add the new model with the existing auto=generated model already there?
Hi Pragim,
ReplyDeleteI 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
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.
ReplyDeleteNow I kept both the classes in the same folder (Model) and I am able to see expected output.
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.
ReplyDeleteusing this can i change the name of any column as different from the table present in SQL server.
ReplyDeleteI am not able to see the designer.cs file.
ReplyDeleteWhen I am trying to change
Code Generation Strategy =T4 to Legacy ObjectContext...I am getting 108 errors.
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
ReplyDeletei 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
ReplyDeletei 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
ReplyDeleteyes for department.cs problems class name departments.cs and after creation rename it to Department in coding area
ReplyDelete