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

"No parameterless constructor defined for this object" when updating grid

1 Answer 410 Views
Grid
This is a migrated thread and some comments may be shown as answers.
pmourfield
Top achievements
Rank 1
pmourfield asked on 30 Apr 2014, 04:28 PM
Good afternoon. My Kendo Grid allows me to create new records, but when I try to update a record I get the following error:

"No parameterless constructor defined for this object"

I should note that if I do not change anything when the update popup appears and just click the update button, it works fine. Only if I change something and then click update do I get the error.

Here is my grid code:

@(Html.Kendo().Grid<ExpenseReport.MVC.Models.ExpenseReportModel>()
        .Name("Grid")
            .Columns(columns =>
            {               
                columns.Bound(p => p.ExpenseReportId).Visible(true);
                columns.Bound(p => p.ExpenseLineItemId).Visible(true);
                columns.Bound(p => p.ExpenseTypeDesc).Title("Expense Type");
                columns.Bound(p => p.City).Title("City");
                columns.Bound(p => p.StateName).Title("State");
                columns.Bound(p => p.Date).Format("{0:d}").Title("Date");
                columns.Bound(p => p.Amount).Title("Amount");
                columns.Bound(p => p.EndingMileage).Title("Ending Mileage");
                columns.Bound(p => p.BeginningMileage).Title("Beginning Mileage");
                columns.Command(command => { command.Edit(); command.Destroy(); });
            })
            .ToolBar(toolbar => toolbar.Create().HtmlAttributes(new { id = "btnAdd" }))
            .Editable(editable => editable.Mode(GridEditMode.PopUp).TemplateName("NewExpense").Window(w => w.Width(500)))
            .Pageable()           
            .Scrollable()           
            .HtmlAttributes(new { style = "height:430px; width=100%" })           
            .DataSource(dataSource => dataSource
                .Ajax()
                .PageSize(20)
                .Events(events => events.Error("error_handler"))
                .Model(model =>
                {
                    model.Id(p => p.ExpenseReportId);
                    model.Id(p => p.ExpenseLineItemId);
                })
                .Create(create => create
                    .Action("EditingPopup_Create", "ExpenseReport")
                    .Data("erLineItemsCreateData"))
                //.Read(read => read
                //    .Action("EditingPopup_Read", "ExpenseReport")
                //    .Data("erLineItemsReadData"))
                .Update(update => update
                    .Action("EditingPopup_Update", "ExpenseReport").Type(HttpVerbs.Post)
                    .Data("erLineItemsUpdateData"))
                .Destroy(update => update
                    .Action("EditingPopup_Destroy", "ExpenseReport").Type(HttpVerbs.Post)))          
    )
    <script>
        function error_handler(e) {
            if (e.errors) {
                var message = "Errors:\n";
                $.each(e.errors, function (key, value) {
                    if ('errors' in value) {
                        $.each(value.errors, function () {
                            message += this + "\n";
                        });
                    }
                });
                alert(message);
            }
        }       
 
        //pass additional data to the READ action method
        function erLineItemsReadData() {
            return {
                expenseReportId: "@ViewBag.ExpenseReportId"
                };
        }
 
        function erLineItemsCreateData() {
            return {
                expenseReportId: "@ViewBag.ExpenseReportId"
                };
        }
 
        function erLineItemsUpdateData() {
            return {
                expenseReportId: "@ViewBag.ExpenseReportId",
                expenseLineItemId: "@ViewBag.ExpenseLineItemId"
                };
        }


Here is the controller code:


[AcceptVerbs(HttpVerbs.Post)]
        public ActionResult EditingPopup_Update([DataSourceRequest] DataSourceRequest request, ExpenseReportModel erLineItem, int expenseReportId, int expenseLineItemId)
        {
            if (erLineItem != null && ModelState.IsValid)
            {
                globalKip.UpdateExpenseReportLineItem(expenseReportId, erLineItem.ExpenseTypeDesc, erLineItem.Date, erLineItem.Amount, erLineItem.City, erLineItem.StateName, erLineItem.EndingMileage, erLineItem.BeginningMileage);
            }
             
            return Json(new[] { erLineItem }.ToDataSourceResult(request, ModelState));
        }


Does anyone know what's going on here? 

1 Answer, 1 is accepted

Sort by
0
Vladimir Iliev
Telerik team
answered on 01 May 2014, 01:16 PM
Hi,

I already answered to this query in duplicated support ticket created by you - #815173. Please keep in mind that it is highly recommended that you keep related questions in one support thread or a forum post, so that we can easily keep track of your support history and provide better answers in a shorter time.

For convenience I also included the response to the current thread:

Basically the described behavior is caused by invalid models IDs - from the provided information it seems that you are trying to set two different fields for primary key which currently is not supported. In current case I can only suggest to create new ViewModel which to combine this two keys in single property.


Regards,
Vladimir Iliev
Telerik
 

Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

 
Tags
Grid
Asked by
pmourfield
Top achievements
Rank 1
Answers by
Vladimir Iliev
Telerik team
Share this question
or