c# - Prevent ASP.NET MVC CreateEdit Templates from using parameters - Stack Overflow

admin2025-04-28  2

My model has certain parameters that are read only.

I use a database first approach and use stored procedures for all calls to database.

Examples of parameters in the class that I do not want being added when using the template are DateCreated, or the Id columns that are identity (auto num), or a value that is not in a database but is returned from a stored procedure.

Is there a way using the System.ComponentModel.DataAnnotations to prevent these fields from appearing when I click Add > View > MVC 5 View > and select Create or Edit Template, then choose that particular model?

I have tried using [Editable(false)] annotation on the models but that doesn't work.

I was hoping that those fields would not be shown in view, thus having me delete them later

My model has certain parameters that are read only.

I use a database first approach and use stored procedures for all calls to database.

Examples of parameters in the class that I do not want being added when using the template are DateCreated, or the Id columns that are identity (auto num), or a value that is not in a database but is returned from a stored procedure.

Is there a way using the System.ComponentModel.DataAnnotations to prevent these fields from appearing when I click Add > View > MVC 5 View > and select Create or Edit Template, then choose that particular model?

I have tried using [Editable(false)] annotation on the models but that doesn't work.

I was hoping that those fields would not be shown in view, thus having me delete them later

Share Improve this question edited Jan 9 at 18:26 Miranda asked Jan 9 at 18:06 MirandaMiranda 13511 bronze badges 1
  • 1 I would suggest segregating your Entities from your View Models and mapping between the two. The view model would include the properties of the entity except for the properties you mentioned in your post. The View model would then be used in your View and when passed to the controller, it would be mapped to an entity for database i/o. Here's a related post: asp-net-mvc-database-entities-or-viewmodels – Ryan Wilson Commented Jan 9 at 18:43
Add a comment  | 

1 Answer 1

Reset to default 0

You can use the [ScaffoldColumn(false)] data annotation. This attribute ensures that the specified property is ignored when scaffolding views for your model. This attribute is intended to exclude properties from the views generated by the scaffolding templates. It works both for the Add View dialog and for runtime dynamic data.

public class ModelX
{
    [ScaffoldColumn(false)] // Prevent this from showing up in templates
    public int Id { get; set; }
}
转载请注明原文地址:http://anycun.com/QandA/1745781767a91212.html