Fun and Struggles with MVC — No Parameterless Constructor Defined
It’s taking a little while, but I’m starting to understand the magic behind model binding in MVC. It’s fairly simply to try it out while watching videos and tutorials that are out there; but when I applied it to our enterprise application with a fairly large collection of domain models that already exist, I had less than ideal results.
Here’s the struggle I was having…
The Problem
I have an entity called a SalesRep which has several properties including a few complex object properties such as EmailAddress , PersonName (struct), and Address (street1, street2, city, state, etc.) as well as many primitives.
I had both GET and POST Create actions defined in the SalesRepController as follows:
/// <summary>
/// Add a new sales rep.
/// </summary&rt;
/// <returns&rt;</returns&rt;
public ActionResult Create()
{
return View(new SalesRep());
}/// <summary>
/// Save a new sales rep.
/// </summary>
/// <param name="salesRep"></param>
/// <returns>If successful, the SalesRep/List/ View. If not successful, the current SalesRep/Create/ view.</returns>
[HttpPost]
public ActionResult Create(SalesRep salesRep)
{
var logic = new SalesLogic();
logic.SaveSalesRep(ref salesRep);return RedirectToAction("List");
}
However, when calling the Create action on the SalesRepController, I would get the following Parameterless constructor error and could never even enter into a breakpoint.
The Solution
After spending quite a bit of time experimenting and searching the internet, I couldn’t readily resolve the issue. Feeling as though I was never going to grasp MVC, and cursing the videos that looked so ridiculously easy, I spun up a new trivial sandbox project. I created new simplistic models, new controllers, and everything worked perfectly. So what made my old enterprise entity classes different? The answer was… the constructors.
As you can see in the above error message it is properly reporting that it could not find a parameterless constructor for the object; but which object? I had been looking at the SalesRep object and even the View and Controllers, but what I should have been looking at was the complex properties within the SalesRep as MVC recursively reflected all of the properties and created them with a parameterless constructor. In my case, we had an EmailAddress which specifies a single constructor:
public EmailAddress(string value) {
//our code
}
It was while MVC was auto-magically wiring up the form elements to the email address object that the action came tumbling down.
A Simple Recreation
I’ve recreated this scenario using a simpler class slightly modified from Scott Allen’s pluralsight demo’s.
Below is a screenshot of my newly created Movie class which contains three properties; Name, Year, and Studio. Name and Year are both primitives, while Studio references another entity.
Here is the Studio class with the pertinent name parameter constructor only. There is no parameterless constructor in this class and because we have specified a constructor the default is overridden.
When we now reference this property within the Create View to allow for model binding we will encounter the error.
We can correct this by adding a parameterless constructor in the Studio class and all is well again.