Using Linq-To-Sql classes as a model and hiding ce

2019-09-16 13:40发布

问题:

I'm using Linq-To-Sql to create all my entity classes.

I have one table, Companies, that just contains a CompanyID and a Name column. The CompanyID is generated automatically by the database when a new record is inserted.

I have a view page that adds a Company to the database. I'm passing my Linq-To-Sql entity to the view page and calling <%= Html.EditorForModel() %> on the page.

My problem is that it includes a textbox for the CompanyID.

Is there a way to hide that? Looking around, I found that you can use a HiddenInputAttribute, but I don't know how to use it on my Linq-To-Sql entity

回答1:

I'm passing my Linq-To-Sql entity to the view page

You should really be use view models. So define a view model class which is specifically tailored to the needs of the given view and containing only the necessary properties. Then the controller could map your model to the view model and pass the view model to the view. This mapping could be simplified with framework such as AutoMapper.



回答2:

You should be able to use the ScaffoldColumnAttribute for CompanyID if you are using Linq-To-Sql entities directly : http://msdn.microsoft.com/en-us/library/system.componentmodel.dataannotations.scaffoldcolumnattribute.aspx