Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to hide Entity Framework entity properties from strongly typed views?

I am using Entity Framework in my ASP.NET MVC 4.0 application and I want to know how to prevent or hide fields from my entity from being generated in my strongly typed view? Right now several primary key fields and timestamp fields are being generated on the view which I do not want.

I know setting the property to internal as opposed to public works but I am not sure of the total downstream effect this will have. I prefer to use data annotations on the properties but the ones I have tried prevent Controller scaffolding or make them as hidden fields. I prefer for them to remain public but just not be generated in the strongly typed view.

EDIT:

To generate a strongly typed View, add a new 'View' in Visual Studio and select the class in the dialog to which the view is modeled after. This in turn will create a view with all of the controls that are represented by properties on the class. For example a LastName field is created as below:

@Html.EditorFor(model => model.FirstName)
like image 451
atconway Avatar asked Aug 14 '12 12:08

atconway


2 Answers

Answer to the question

Attribute

[ScaffoldColumn(false)]

or

[Display(AutoGenerateField=false)]

before the unwanted properties will prevent de designer to generate scaffolding fields for those properties.

like image 138
Pluc Avatar answered Nov 08 '22 21:11

Pluc


To hide a property from the UI via Data Annotations, decorate the property with

 [ScaffoldColumn(false)] 

and they will be ignored by the editor templates.

like image 30
Forty-Two Avatar answered Nov 08 '22 20:11

Forty-Two