Server Error in '/' Application.
--------------------------------------------------------------------------------
No parameterless constructor defined for this object.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.MissingMethodException: No parameterless constructor defined for this object.
Source Error:
Line 16: HttpContext.Current.RewritePath(Request.ApplicationPath, false);
Line 17: IHttpHandler httpHandler = new MvcHttpHandler();
Line 18: httpHandler.ProcessRequest(HttpContext.Current);
Line 19: HttpContext.Current.RewritePath(originalPath, false);
Line 20: }
I was following Steven Sanderson's 'Pro ASP.NET MVC Framework' book. On page 132, in accordance with the author's recommendation, I downloaded the ASP.NET MVC Futures assembly, and added it to my MVC project. [Note: This could be a red herring.]
After this, I could no longer load my project. The above error stopped me cold.
My question is not, "Could you help me fix my code?"
Instead, I'd like to know more generally:
- How should I troubleshoot this issue?
- What should I be looking for?
- What might the root cause be?
It seems like I should understand routing and controllers at a deeper level than I do now.
I had the same problem.
Just Removed
HttpFileCollectionBase files
from Post Action method argument and added likeHttpFileCollectionBase files = Request.Files;
in method body.I'd added a
DropDownList
to my form, however in my case it wasn't (and wasn't intended to be) submitted with the form as it was outside of the<form></form>
tags:As the Model contained the field for display only, this also caused the
No parameterless constructor defined for this object
error because the field wasn't submitted at all.In this case I fixed it by adding an exclude binding:
This happened to me, and the results on this page were a good resource that led me in many directions, but I would like to add another possibility:
As stated in other replies, creating a constructor with parameters removes the implicit parameterless constructor, so you have to explicitly type it.
What was my problem was that a constructor with default parameters also triggered this exception.
Gives errors:
Works:
So I have gotten that message before as well, when doing an ajax call. So what it's basically asking for is a constructor in that model class that is being called by the contoller, doesn't have any parameter.
Here is an example
This can also be caused if your Model is using a SelectList, as this has no parameterless constructor:
You'll need to refactor your model to do it a different way if this is the cause. So using an
IEnumerable<Contact>
and writing an extension method that creates the drop down list with the different property definitions:Or you can use the @Mark and @krilovich approach, just need replace SelectList to IEnumerable, it's works with MultiSelectList too.
I just had a similar problem. The same exception occurs when a
Model
has no parameterless constructor.The call stack was figuring a method responsible for creating a new instance of a model.
Here is a sample: