The callstack shows the following:
[MissingMethodException: No parameterless constructor defined for this object.]
System.RuntimeTypeHandle.CreateInstance(Ru
Did you remove Default.aspx? That needs to be in place to render the root properly. This can also happen if you're trying to use Dependency Injection and your container isn't setup properly - meaning there is no ControllerFactory that asks the DI Container for the instance.
The root of my problem was also SelectList.
I had this:
<%: Html.DropDownListFor(m => Model.Destinations, Model.Destinations)%>
When I should have had this:
<%: Html.DropDownListFor(m => Model.Destination, Model.Destinations)%>
Destination is the user's selection, Destinations is the list of possible values. Using the plural SelectList twice caused the problem.
I solved the issue which is caused of SelectList object because it does not provide the default constructor so we cant have in out ViewModel.
For those Googling this exception, here is a more general way to diagnose it:
The only easy way I've found to diagnose this problem is to override MVC as close to the exception as possible with your own code. Then your code will break inside Visual Studio when this exception occurs, and you can read the Type causing the problem from the stack trace.
This seems like a horrible way to approach this problem, but it's very fast, and very consistent.
For example, if this error is occurring inside the MVC DefaultModelBinder (which you will know by checking the stack trace), then replace the DefaultModelBinder with this code:
public class MyDefaultModelBinder : System.Web.Mvc.DefaultModelBinder
{
protected override object CreateModel(System.Web.Mvc.ControllerContext controllerContext, System.Web.Mvc.ModelBindingContext bindingContext, Type modelType)
{
return base.CreateModel(controllerContext, bindingContext, modelType);
}
}
And update your Global.asax.cs:
public class MvcApplication : System.Web.HttpApplication
{
...
protected void Application_Start(object sender, EventArgs e)
{
ModelBinders.Binders.DefaultBinder = new MyDefaultModelBinder();
}
}
Now the next time you get that exception, Visual Studio will stop inside your MyDefaultModelBinder class, and you can check the "modelType" property to see what type caused the problem.
The example above works for when you get the "No parameterless constructor defined for this object" exception during model binding, only. But similar code can be written for other extension points in MVC (e.g. controller construction).
I also have properties on my view model class that return SelectList instances. I decorated my class with the Bind attribute to exclude these properties like this:
[Bind(Exclude = "Countries")]
public class MyViewModel
{
...
public SelectList Countries { get; set; }
}
Hope that helps, Roger
Your custom classes in the action args must have a parameterless constructor in order to use the default model binder. Otherwise, either (1) create a custom model binder or (2) pass primitive types instead of custom classes to the Action.