MVC模型绑定不适用于Kendo Grid

时间:2012-12-06 17:17:34

标签: asp.net-mvc kendo-ui

在MVC项目中,我有以下视图,其中我使用了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")))
%>

我还有一个控制器,我想在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
    }

问题是Milestone类型(控制器中的参数)始终为null,即使我在网格中创建新条目并按保存时输入数据。知道怎么做才能在里程碑参数中传递输入的数据?提前谢谢

1 个答案:

答案 0 :(得分:12)

正如我在Fiddler中看到的那样,参数是以models前缀发布的,因此你必须像这样访问它们:

public ActionResult CreateMilestone([DataSourceRequest] DataSourceRequest request,
    [Bind(Prefix="models")] List<Milestone> milestons) 
{
    Milestone milestone = milestons[0];
    .
    .
    .  
}