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

狂风中的少年 提交于 2019-12-12 08:06:58

问题


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)

回答1:


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.




回答2:


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.




回答3:


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



来源:https://stackoverflow.com/questions/11952740/how-to-hide-entity-framework-entity-properties-from-strongly-typed-views

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!