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

I use Entity Framework in my ASP.NET MVC 4.0 application and I want to know how to prevent or hide fields from my object that are generated in my strongly typed view? At the moment, in the view that I don't want, several primary key fields and timestamp fields are generated.

I know that setting the internal property, as opposed to public works, but I'm not sure if this will have the effect of a subsequent stream. I prefer to use property data annotations, but the ones I tried prevent Controller scaffolds or make them hidden. I prefer that they remain open, but simply are not generated in a strongly typed form.

EDIT:

To create a strongly typed view, add a new "view" in Visual Studio and select the class in the dialog box with which the image is modeled. This, in turn, will create a view with all the controls that are represented by the properties of the class. For example, the LastName field is created as follows:

 @Html.EditorFor(model => model.FirstName) 

Does anyone know how to do this?

Thanks!

+7
source share
3 answers

Answer the question

Attribute

 [ScaffoldColumn(false)] 

or

 [Display(AutoGenerateField=false)] 

before unwanted properties will prevent the developer from creating fields for forests for these properties.

+19
source

To hide a property from the user interface via data annotations, decorate the property with

  [ScaffoldColumn(false)] 

and they will be ignored by the editor templates.

+7
source

You should use separate ViewModel classes that contain only the properties you want.

+4
source

All Articles