Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET MVC - Alternative for [Bind(Exclude = "Id")]

Tags:

Is there an alternative for [Bind(Exclude = "Id")] (Related Question) ?

Could I write a model binder?

like image 982
Rookian Avatar asked Jan 07 '11 22:01

Rookian


1 Answers

Yes there is: it's called view models. View models are classes which are specifically tailored to the specific needs of a given view.

So instead of:

public ActionResult Index([Bind(Exclude = "Id")] SomeDomainModel model) 

use:

public ActionResult Index(SomeViewModel viewModel) 

where the view model contains only the properties which need to be bound. Then you could map between the view model and the model. This mapping could be simplified with AutoMapper.

As best practice I would recommend you to always use view models to and from a view.

like image 76
Darin Dimitrov Avatar answered Sep 17 '22 15:09

Darin Dimitrov