ASP.NET MVC3: how to hide fields using Html.DisplayForModel and Html.EditorForModel

Using the following two methods in the view of viewing and editing, they start the model and automatically create a list of fields with labels for display / editing.

@Html.EditorForModel() @Html.DisplayForModel() 

The problem is that they display each field. Including an identifier field, which is bad when it comes to editing, since it cannot be changed.

So how to hide certain fields in the model. (without removing fields from the model.)

+8
c # asp.net-mvc asp.net-mvc-3
source share
1 answer

You can use the Scaffold attribute

 [ScaffoldColumn(false)] public int Id {get;set;} 

And if you also want the model’s middleware not to touch the identifier field (to prevent overwriting), you can also set ReadOnly -Attribute.

 [ReadOnly(true)] [ScaffoldColumn(false)] public int Id {get;set;} 
+12
source share

All Articles