简体   繁体   中英

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)

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.

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.

您应该使用仅包含所需属性的单独ViewModel类。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM