griZZZly8
griZZZly8

Reputation: 694

ASP.NET MVC model binding and default constructor

How do bind my domain object Car to View?

MVC says "your class must have default constructor". But I don't want to change any business rules by creating a default constructor. The only solution I can see - is to use CarView in my View and then map it to Car.

P.S. NHibernate wants a default constructor too, but it can be protected. This I can do.

Upvotes: 1

Views: 3002

Answers (2)

MrKWatkins
MrKWatkins

Reputation: 2658

You could create the object yourself and call UpdateModel to do the binding instead:

public ActionResult MyAction()
{
   var car = new MyCar(somethingToPassIntoTheConstructor);

   UpdateModel(car);

   // Do stuff with car.
}

Upvotes: 1

Mahesh Velaga
Mahesh Velaga

Reputation: 21991

IMHO, its a good idea to seperate the objects that go to your view from you domain objects. This has a long list of advantages (which I am not going into now).

You can then use Automapper to map your view models to your domain objects

Upvotes: 1

Related Questions