Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MVC Model Binding does not work on Kendo Grid

In an MVC project i have the following view where i use the Kendo Grid

<%: Html.Kendo().Grid<Milestone>()
                .Name("MilestonesGrid")
                .Columns(columns =>
                {
                    columns.Bound(p => p.ContractMilestoneID).Hidden();
                    columns.Bound(p => p.MilestoneSN).Title("Κωδικός οροσήμου");
                    columns.Bound(p => p.EstimatedDate).Title("Εκτιμώμενη ημερομηνία");
                    columns.Bound(p => p.RealDate).Title("Πραγματική ημερομηνία");
                    columns.Bound(p => p.MilestoneDescription).Title("Περιγραφή");
                    columns.Bound(p => p.Payment).Title("Πληρωμή");
                    columns.Bound(p => p.PaymentRate).Title("Ποσοστό πληρωμής");
                    columns.Bound(p => p.IsCompleted).Title("Έχει ολοκληρωθεί");
                    columns.Command(command =>
                        {
                            command.Edit()
                                   .Text("Επεξεργασία")
                                   .CancelText("Ακύρωση")
                                   .UpdateText("Αποθήκευση");

                            command.Destroy()
                                   .Text("Διαγραφή");
                        });
                })
                .ToolBar(toolbar => toolbar.Create().Text("Προσθήκη νέου οροσήμου"))
                .Editable(editable => editable.Mode(GridEditMode.InLine))
                .Sortable()
                .Pageable()
                .Filterable()
                .Resizable(resize => resize.Columns(true))
                .DataSource(dataSource => dataSource
                    .Ajax()
                    .ServerOperation(true)
                    .Model(model => model.Id(o => o.ContractMilestoneID))
                    .Model(model => model.Field(o => o.MilestoneSN))
                    .Model(model => model.Field(o => o.EstimatedDate))
                    .Model(model => model.Field(o => o.RealDate))
                    .Model(model => model.Field(o => o.MilestoneDescription))
                    .Model(model => model.Field(o => o.Payment))
                    .Model(model => model.Field(o => o.PaymentRate))
                    .Model(model => model.Field(o => o.IsCompleted))
                    .Batch(true)
                    .Create(create => create.Action("CreateMilestone", "Milestones"))
                    .Read(read => read.Action("DetailsJson", "Milestones", 
                        new { id = ViewBag.ID }))
                    .Update(update => update.Action("UpdateMilestone", "Milestones"))
                    .Destroy(delete => delete.Action("DeleteMilestone", "Milestones")))
%>

Also i have a controller where i want to save a new entry in the Kendo Grid.

[HttpPost]
    public ActionResult CreateMilestone([DataSourceRequest] DataSourceRequest request, 
        Milestone milestone)
    {
        if (milestone != null && ModelState.IsValid)
        {
            using (TADCEntities database = new TADCEntities())
            {
                tblSymvaseisOrosima item = new tblSymvaseisOrosima
                {
                    fldEstimatedDate = milestone.EstimatedDate,
                    fldIsCompleted = milestone.IsCompleted,
                    fldMilestoneDescription = milestone.MilestoneDescription,
                    fldMilestoneSN = milestone.MilestoneSN,
                    fldPayment = milestone.Payment,
                    fldPaymentRate = milestone.PaymentRate,
                    fldRealDate = milestone.RealDate,
                    fldStoixeioYpoergouID = milestone.ElementSubProjectID
                };

                database.tblSymvaseisOrosima.Add(item);
                database.SaveChanges();

                return Json(new[] { item }.ToDataSourceResult(request, ModelState));
            }
        }

        return View();
        // should also return json
    }

The problem is that the Milestone type (the parameter in the controller) is always null even if i enter data when i create a new entry in the grid and press save. Any idea what to do in order to pass the entered data in the milestone parameter? Thank you in advance

like image 920
Giorgos Manoltzas Avatar asked Dec 06 '12 17:12

Giorgos Manoltzas


1 Answers

As I saw in the Fiddler the parameters is posting with models prefix, thus you must access them like this:

public ActionResult CreateMilestone([DataSourceRequest] DataSourceRequest request,
    [Bind(Prefix="models")] List<Milestone> milestons) 
{
    Milestone milestone = milestons[0];
    .
    .
    .  
}
like image 58
Behnam Esmaili Avatar answered Nov 17 '22 06:11

Behnam Esmaili