asp.net-mvc – MVC Model Binding不适用于Kendo Grid

前端之家收集整理的这篇文章主要介绍了asp.net-mvc – MVC Model Binding不适用于Kendo Grid前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
在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.知道怎么做才能在里程碑参数中传递输入的数据?先感谢您

解决方法

正如我在Fiddler中看到的那样,参数是使用模型前缀发布的,因此您必须像这样访问它们:
public ActionResult CreateMilestone([DataSourceRequest] DataSourceRequest request,[Bind(Prefix="models")] List<Milestone> milestons) 
{
    Milestone milestone = milestons[0];
    .
    .
    .  
}
原文链接:https://www.f2er.com/aspnet/248872.html

猜你在找的asp.Net相关文章