This is a migrated thread and some comments may be shown as answers.

System.MissingMethodException: No parameterless constructor defined for this object

5 Answers 987 Views
Grid
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Patrick
Top achievements
Rank 1
Patrick asked on 22 Mar 2012, 09:03 PM
We have a grid which work fine when inserting stuff but once saved has a different behavior.

When we are editing an existing element in the grid, an Ajax is created to the server but the server crash with the exception:
<br>System.MissingMethodException: No parameterless constructor defined for this object.<br>   at System.RuntimeTypeHandle.CreateInstance(RuntimeType type, Boolean publicOnly, Boolean noCheck, Boolean& canBeCached, RuntimeMethodHandleInternal& ctor, Boolean& bNeedSecurityCheck)<br>   at System.RuntimeType.CreateInstanceSlow(Boolean publicOnly, Boolean skipCheckThis, Boolean fillCache)<br>   at System.RuntimeType.CreateInstanceDefaultCtor(Boolean publicOnly, Boolean skipVisibilityChecks, Boolean skipCheckThis, Boolean fillCache)<br>   at System.Activator.CreateInstance(Type type, Boolean nonPublic)<br>   at System.Web.Mvc.DefaultModelBinder.CreateModel(ControllerContext controllerContext, ModelBindingContext bindingContext, Type modelType)<br>   at System.Web.Mvc.DefaultModelBinder.BindComplexModel(ControllerContext controllerContext, ModelBindingContext bindingContext)<br>   at System.Web.Mvc.DefaultModelBinder.BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)<br>   at System.Web.Mvc.DefaultModelBinder.GetPropertyValue(ControllerContext controllerContext, ModelBindingContext bindingContext, PropertyDescriptor propertyDescriptor, IModelBinder propertyBinder)<br>   at System.Web.Mvc.DefaultModelBinder.BindProperty(ControllerContext controllerContext, ModelBindingContext bindingContext, PropertyDescriptor propertyDescriptor)<br>   at System.Web.Mvc.DefaultModelBinder.BindProperties(ControllerContext controllerContext, ModelBindingContext bindingContext)<br>   at System.Web.Mvc.DefaultModelBinder.BindComplexElementalModel(ControllerContext controllerContext, ModelBindingContext bindingContext, Object model)<br>   at System.Web.Mvc.DefaultModelBinder.BindComplexModel(ControllerContext controllerContext, ModelBindingContext bindingContext)<br>   at System.Web.Mvc.DefaultModelBinder.BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)<br>   at System.Web.Mvc.DefaultModelBinder.UpdateCollection(ControllerContext controllerContext, ModelBindingContext bindingContext, Type elementType)<br>   at System.Web.Mvc.DefaultModelBinder.BindComplexModel(ControllerContext controllerContext, ModelBindingContext bindingContext)<br>   at System.Web.Mvc.DefaultModelBinder.BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)<br>   at System.Web.Mvc.ControllerActionInvoker.GetParameterValue(ControllerContext controllerContext, ParameterDescriptor parameterDescriptor)<br>   at System.Web.Mvc.ControllerActionInvoker.GetParameterValues(ControllerContext controllerContext, ActionDescriptor actionDescriptor)<br>   at System.Web.Mvc.ControllerActionInvoker.InvokeAction(ControllerContext controllerContext, String actionName)}

The grid use Ajax Binding:

        .DataBinding(dataBinding =>
            dataBinding.Ajax()
               .Select(
"LoadDetailBatchEditing", "Invoice", new { itemType = InvoiceDetailType.Service })
               .Update(
"UpdateDetailBatchEditing", "Invoice", new { itemType = InvoiceDetailType.Service })<br>        )
In fact, the UpdateDetailBatchEditing seem to be problematic since it does a call (I can see the XHR call in the debug window) but doesn't look to be able to do its binding on the controller action "UpdateDetailBatchEditing".



[AcceptVerbs(HttpVerbs.Post)]
        [GridAction]
        public ActionResult UpdateDetailBatchEditing(
            [Bind(Prefix = "inserted")] IEnumerable<InvoiceDetail> insertedDetails,
            [Bind(Prefix = "updated")] IEnumerable<InvoiceDetail> updatedDetails,
            [Bind(Prefix = "deleted")] IEnumerable<InvoiceDetail> deletedDetails, InvoiceDetailType itemType) {
//...


Why do I have a 
   System.MissingMethodException: No parameterless constructor defined for this object

Thank you

5 Answers, 1 is accepted

Sort by
0
Phil
Top achievements
Rank 1
answered on 29 Mar 2012, 03:23 PM
I'm having the same problem.

I'm binding my grid to a collection of POCO objects.

In my domain I have a Project class. I had no parameterless constructor on it (because my Project class must be hydrated correctly via a factory class). By adding a parameterless constructor and allowing illegal construction of a project - the Telerik problem goes away.

I suspect your InvoiceDetailType class needs a parametrless constructor added.

However I don't want a parameterless constructor that is public. I made an internal one but the problem persists. Looks like you are forced to provide a public parameterless constructor. Did you get a resolution.

regards

Phil
0
Daniel
Telerik team
answered on 30 Mar 2012, 04:41 PM
Hello All,

If editing is enabled the Grid tries to create a default item using the model parameterless constructor. The MVC DefaultModelBinder also needs it to create the object when binding the model in the action method. Creating a view model that exposes such constructor should solve the problem. An alternative is to specify the default item in the Grid's Editable configuration:
.Editable(eidting =>
{
    eidting.DefaultDataItem(new MyModel("constructor value"));
})
and register a custom model binder which can create an instance of the model:
public class CustomModelBinder : DefaultModelBinder
{
    protected override object CreateModel(ControllerContext controllerContext, ModelBindingContext bindingContext, Type modelType)
    {
        return new MyModel("constructor value");
    }
}
//Global.asax
 
protected void Application_Start()
{
    ModelBinders.Binders.Add(typeof(MyModel), new CustomModelBinder());


Kind regards,
Daniel
the Telerik team
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the Telerik Extensions for ASP.MET MVC, subscribe to their blog feed now.
0
Patrick
Top achievements
Rank 1
answered on 05 Apr 2012, 04:26 PM
The solution is really not what has been provided here by Telerik but I'll show you how to fix this problem.

The problem was in the controller signature. We has to have the previx in the name of the method parameter with also the type inside the name of what type

 [Bind(Prefix = "inserted")] IEnumerable<MyTypeHereName> insertedMyTypeHereName

You cannot do :

 [Bind(Prefix = "inserted")] IEnumerable<MyTypeHereName> insertedWhatYouWant

I do not get it why it works like that since we specify the type in the enumerable. Big waste of time. Hope it helps others.
0
Phil
Top achievements
Rank 1
answered on 11 Apr 2012, 08:00 AM
Hi Daniel,

Microsoft's MVC binding works with internal parameterless constructors on POCO objects. Telerik grid in edit mode does not. You need public parameterless constructors. I have removed all telerik editing from my grid and it all works. (Creates, deletes and updates now being done via custom functions and the Telerik Window control)

Phil
0
Daniel
Telerik team
answered on 12 Apr 2012, 08:30 PM
Hello Phil,

Yes, the Grid needs a public parameterless constructor to create an item that is used on the client. As in my previous reply, I can suggest to use the DefaultDataItem method to create it in order to avoid the exception.
If the problem persists, please sent a runnable sample that reproduces the problem so I can investigate further.

All the best,
Daniel
the Telerik team
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the Telerik Extensions for ASP.MET MVC, subscribe to their blog feed now.
Tags
Grid
Asked by
Patrick
Top achievements
Rank 1
Answers by
Phil
Top achievements
Rank 1
Daniel
Telerik team
Patrick
Top achievements
Rank 1
Share this question
or