8

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

3

1 Answer 1

12

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];
    .
    .
    .  
}
1
  • 3
    Yes this is correct. And it is needed because of the .Batch(true) setting of the grid. And the solution is also described in the kendo ui documentation.
    – nemesv
    Dec 6, 2012 at 21:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.