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

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.


You can exclude properties directly with an attribute using;

[BindNever]

A very simple solution that I figured out.

public ActionResult Edit(Person person)
{
    ModelState.Remove("Id"); // This will remove the key 

    if (ModelState.IsValid)
       {
           //Save Changes;
       }
    }
}