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.
Thank you! Had this problem too and was in rage with this…
Thanks, spot on.
If you can’t have a parameterless constructor, you must create a custom model binder.
we can use parameter constructor. when you watch Ninject video, you can know it is working very well
You are a “Guardian Angel”!!!!! I was going nuts over this one. You saved some real hustle. Thanks a million
thanks alot
I visited many websites however the audio feature for audio songs current at this
web site is actually marvelous.
What’s Taking place i’m new to this, I stumbled upon
this I have discovered It positively useful and it has aided me out loads.
I am hoping to contribute & assist different customers like its aided me.
Great job.
I don’t even understand how I stopped up here, but I thought this submit was once good.
I don’t recognize who you are however definitely you’re going to a famous blogger if you are
not already. Cheers!