简体   繁体   中英

ASP.NET MVC3: How do I hide fields using Html.DisplayForModel and Html.EditorForModel

Using the following two methods in my view and edit views, They run off the model and automatically generate a list of the fields with labels, to display/edit.

  @Html.EditorForModel()

  @Html.DisplayForModel()

The problem is that they display every field.. Including the ID field, which is bad when it comes to editing as it should not be changed.

So, How do I hide certain fields in the model. (without removing the fields from the model.)

You can use the Scaffold Attribute

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

And if you also want that the model binder does not touch the Id field (to prevent over-posting) you can set the ReadOnly -Attribute too.

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

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