首页 > 解决方案 > Kendo Telerik MVC 绑定到子网格的属性不起作用

问题描述

我刚开始使用 Telerik Grids,我不知道这是否是正确的方法,但我的老板想在同一个网格中编辑数据内联,现在我遇到了一个问题:

这是我的数据模型:

        public class InternVM
        {
            public int InternId { get; set; }
            public Guid UserId { get; set; }

            [Required]
            [MaxLength(50)]
            [Display(Name = "User ID")]
            public string UserName { get; set; }

            [Required]
            [MaxLength(50)]
            public string LastName { get; set; }

            [Required]
            [MaxLength(50)]
            public string FirstName { get; set; }

            [UIHint("Date")]
            [Display(Name ="Certified")]
            public DateTime? CertifiedDate { get; set; }
            public string Status { get; set; }

            [UIHint("InternSchedule")]
            public List<InternScheduleVM> Schedules { get; set; }

            [UIHint("InternAttorneyDDL")]
            public List<AttorneyVM> Attorneys { get; set; }
        }

我创建了一个这样的主网格:

@(Html.Kendo().Grid<InternVM>()
      .Name("InternGrid")
      .Columns(columns =>
      {
          columns.Command(command => { command.Edit().Text(" ").CancelText(" ").UpdateText(" "); command.Destroy().Text(" "); })
            .Width(100).Locked(true).Lockable(false);
          columns.Bound(c => c.InternId).Visible(false);
          columns.Bound(c => c.UserName).Width(200);
          columns.Bound(c => c.LastName).Width(200);
          columns.Bound(c => c.FirstName).Width(200);
          columns.Bound(c => c.Attorneys).ClientTemplate("#=DisplayAttorneys(Attorneys)#").Width(200);
          columns.Bound(c => c.Status).Width(200);
          columns.Bound(c => c.CertifiedDate).Format("{0:dd/MM/yyyy}").Width(200);
          columns.Bound(c => c.Schedules).ClientTemplate("#=DisplaySchedules(Schedules)#").Width(700);
      })
      .Scrollable()
      .DataSource(dataSource => dataSource
          .Ajax()
          .Model(m => {
              m.Id(c => c.InternId);
              m.Field(c => c.UserId).DefaultValue(Guid.NewGuid());
              m.Field(c => c.Attorneys).DefaultValue(new List<AttorneyVM>());
          })
          .Read(read => read.Action("Read", "Intern"))
          .Destroy(destroy => destroy.Action("Destroy", "Intern"))
          .Update(update => update.Action("Update", "Intern"))
          .Create(create => create.Action("Create", "Intern"))
          .Events(events => events.RequestEnd("onRequestEnd"))
      )
      .ToolBar(toolbar => { toolbar.Create().Text("Add"); })
      .Editable(editable => editable.Mode(GridEditMode.InLine))
      .HtmlAttributes(new { @style = "height:450px"})
      .Events(e => e.BeforeEdit("onBeforeEdit"))
 )

其中一个字段是使用 EditorTemplateView,它是另一个像这样的网格(计划):

            @model IEnumerable<InternScheduleVM>
        <script>
            function onRequestEnd(e) {
                if (e.type == "update" || e.type == "create" || e.type == "remove") {
                    $("#InternScheduleGrid").data("kendoGrid").dataSource.read();
                }
            }

            function onRequestStart(e) {
                if (e.type == "read")
                    //console.log("onRequestStart");
            }

            function getParentId() {
                let parentId = $('#parentIdInput').val();
                return {
                    internId: parentId
                };
            }

        </script>
        <p>
        </p>
        <div>
            @(Html.Kendo().Grid(Model)
                .Name("InternScheduleGrid")
                .Columns(columns =>
                {
                    columns.Command(command => { command.Edit().Text(" ").CancelText(" ").UpdateText(" "); command.Destroy().Text(" "); }).Width(100);
                    columns.Bound(c => c.InternScheduleID).Visible(false);
                    columns.Bound(c => c.DayOfWeek);
                    columns.Bound(c => c.StartTime).Format("{0:HH:mm:ss}");
                    columns.Bound(c => c.EndTime).Format("{0:HH:mm:ss}");
                })
                .Scrollable()
                .DataSource(dataSource => dataSource
                    .Ajax()
                    .Model(m =>
                    {
                        m.Id(c => c.InternScheduleID);
                    })
                    .Read(read => read.Action("Read", "InternSchedule").Data("getParentId"))
                    .Destroy(destroy => destroy.Action("Destroy", "InternSchedule"))
                    .Update(update => update.Action("Update", "InternSchedule"))
                    .Create(create => create.Action("Create", "InternSchedule").Data("getParentId"))
                    .Events(events => { events.RequestEnd("onRequestEnd"); events.RequestStart("onRequestStart"); })
                )
                .ToolBar(toolbar => { toolbar.Create().Text("Add"); })
                .Editable(editable => editable.Mode(GridEditMode.InLine))
                .Events(e => e.Edit("onEdit"))

            )
        </div>

子网格工作正常,我将它与模型绑定,但每当我在此网格中进行更改时,它不会上传我的模型实习生VM。任何想法为什么?

标签: model-view-controllergridtelerik

解决方案


我只是使用网格详细信息模板从主视图传递参数。谢谢


推荐阅读