1

我目前正在使用带有 Razor 引擎的 MVC4 KendoUI 项目 C#。在网上搜索了很多之后,我找不到实现此目的的方法:

我有一个基于我的模型的剑道网格,它是“卫星”对象的集合,具有一些属性,其中一个属性是“转发器”对象的列表。

我的“卫星”视图有 CRUD,它包含在一个可编辑的剑道网格中,这个网格不显示所有字段,我希望该行为处于可编辑模式,您可以在其中编辑和更新其所有属性,所以我想出了一个编辑器模板,然后当用户单击“编辑”按钮时,我会显示一个自定义部分视图,其中包含要更新的所有字段。这对我来说效果很好,除非我为“转发器”列表创建另一个网格,但它似乎无法正常工作。我能够加载网格,成功执行读取操作,但是当我单击例如“转发器”网格内的“删除”按钮时,该项目从网格中消失,但不会进入控制器并且在服务器端什么也不做. 如果我再次更新和编辑,该项目仍然存在。

我也想知道你们是否认为我正在做正确的方法,或者你是否知道更直接的方法来做到这一点。

我发现最相似的场景是这个Example

这不完全是,但我想告诉你我假装要达到的目标。

这是“卫星”CRUD 视图:

@model IEnumerable<Test_MVC_DTV.Models.Satelite>

@{
    ViewBag.Title = "Satelites";
}

<style>
        #organizer { 
            width: auto;
            margin: 0 auto;
        }

    .k-edit-form-container { width: auto; height: auto;}
    </style>


<div id="organizer">
        @(Html.Kendo().PanelBar()
            .Name("panelbar")
            .ExpandMode(PanelBarExpandMode.Multiple)
            .HtmlAttributes(new { style = "min-width: 800px; width: auto;margin: auto;" })
            .Items(panelbar =>
                {
                    panelbar.Add()
                            .Text("Satélites")
                            .Expanded(true)

                            .Content(@<div style="padding: 10px;">
                                          <section>
                                              Nombre: <input style="margin-left: 7px" id="txtNombre"/> 
                                              <button style="margin-left: 10px" class="k-button" id="btnBuscar">BUSCAR</button>

                                          </section>
                                      </div>);

                })

        ) 
    </div>

@(Html.Kendo().Grid(Model)    
      .Name("grdSatelites")
      .Columns(columns =>
          {
              columns.Bound(p => p.SateliteId).Groupable(false);
              columns.Bound(p => p.Denominacion);
              columns.Bound(p => p.Comentario);

              columns.Command(cmd =>
                  {
                      cmd.Edit().Text("Editar").CancelText("Cancelar").UpdateText("Actualzar");
                      cmd.Destroy().Text("Eliminar");

                  })
                .HtmlAttributes(new {style="text-align:center;"})
                .Width(210);

          })
        .Editable(editable =>
          {
              editable.Mode(GridEditMode.PopUp).Window(wdw=>wdw.Title("Editor Satelite"));
              editable.TemplateName("SatelitesEditor");

          })
      .Events(e=>e.Edit("OnEdit"))
      .Pageable()
      .Sortable()      
      .Scrollable()
      .Filterable()
      .Selectable()
      .ToolBar(toolbar => toolbar.Create().Text("Agregar") )

      .DataSource(dataSource => dataSource 
                                    .Ajax()
                                    .PageSize(5)
                                    .ServerOperation(false)
                                    .Model(model =>
                                    {
                                        model.Id(p => p.SateliteId);
                                        model.Field(p => p.SateliteId).Editable(false);
                                    })
                                    .Read(read => read.Action("Satelites_Read", "Home").Type(HttpVerbs.Post))
                                    .Create(create => create.Action("Satelites_Create", "Home"))
                                    .Destroy(destroy => destroy.Action("Satelites_Delete", "Home"))
                                    .Update(update => update.Action("Satelites_Update", "Home"))
      )

    )

<script>

    $(function () {

        $("#btnBuscar").click(function () {
            var $filter = new Array();
            var $nombre = $("#txtNombre").val();
            if ($nombre) {
                $filter.push({ field: "Nombre", operator: "contains", value: $nombre });
            }
            var grid = $("#kendoGrid").data("kendoGrid");
            grid.dataSource.filter($filter);
        });
    });

    function OnEdit(e) {
        $('#grdTransponders').data().kendoGrid.dataSource.read({ SateliteId: e.model.SateliteId });

    }
</script>

这是编辑器模板:

@model Test_MVC_DTV.Models.Satelite

<div style="padding: 10px; margin: 10px">
            @Html.Label("Denominación:")
            @Html.EditorFor(m => m.Denominacion )

            @Html.Label("Ubicacion Orbital:")
            @Html.EditorFor(m => m.UbicacionOrbital )

            @Html.Label("Comentario:")
            @Html.EditorFor(m => m.Comentario )



    @(Html.Kendo().Grid(Model.Transponders)
            .Name("grdTransponders")
            .Columns(columns =>
            {
                columns.Bound(p => p.TransponderID).Groupable(false);
                columns.Bound(p => p.Polaridad);
                columns.Bound(p => p.Haz);
                columns.Bound(p => p.UsuarioAsignado);
                columns.Bound(p => p.color);
                columns.Command(cmd =>
                {
                    cmd.Edit().Text("Editar").UpdateText("Actualizar").CancelText("Cancelar");
                    cmd.Destroy().Text("Eliminar");

                })
                         .HtmlAttributes(new { style = "text-align:center;" })
                         .Width(210);

            })
            .Editable(editable => {
                                      editable.Mode(GridEditMode.PopUp);
                                      editable.TemplateName("TranspondersEditor");
            })

            .ToolBar(toolbar => toolbar.Create().Text("Agregar") )
            .AutoBind(false)
            .DataSource(dataSource => dataSource
                .Ajax()
                .PageSize(5)
                .ServerOperation(false)               
                .Model(model =>
                    {
                        model.Id(p => p.TransponderID);
                        model.Field(p => p.TransponderID).Editable(false);
                    })
                .Read(read => read.Action("Transponders_Read", "Home").Type(HttpVerbs.Post))
                .Create(create => create.Action("Transponders_Create", "Home"))
                .Destroy(destroy => destroy.Action("Transponders_Delete", "Home"))
                .Update(update => update.Action("Transponders_Update", "Home"))
            )
            .Pageable()

    )
    </div>

我的卫星和转发器操作控制器

region Transponders

        public ActionResult ABMTrandpondersParaSatelite(int? SateliteId)
        {
            return View(listaSatelites.Single(x => x.SateliteId == SateliteId));
        }


        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Transponders_Read([DataSourceRequest] DataSourceRequest request, int SateliteId)
        {

            return Json(listaSatelites.Single(x=>x.SateliteId==SateliteId).Transponders.ToDataSourceResult(request), JsonRequestBehavior.AllowGet);
        }

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Transponders_Update([DataSourceRequest] DataSourceRequest request, Transponder transponder, int IdSatelite)
        {
            if (transponder != null && ModelState.IsValid)
            {
                // var satelites = (List<Satelite>)GetSatelites();
                var satelite = listaSatelites.Single(o => o.SateliteId == IdSatelite);
                var transponderTarget = satelite.Transponders.Single(o => o.TransponderID == transponder.TransponderID);

                if (transponderTarget != null)
                {
                    int targetIndex = satelite.Transponders.IndexOf(transponderTarget);
                    satelite.Transponders[targetIndex].TransponderID = transponder.TransponderID;
                    satelite.Transponders[targetIndex].Polaridad = transponder.Polaridad;
                    satelite.Transponders[targetIndex].Haz = transponder.Haz;
                    satelite.Transponders[targetIndex].UsuarioAsignado = transponder.UsuarioAsignado;
                    satelite.Transponders[targetIndex].NumeroTranslacionIRD = transponder.NumeroTranslacionIRD;
                    satelite.Transponders[targetIndex].Comentario = transponder.Comentario;
                    satelite.Transponders[targetIndex].FrecuenciaBajada = transponder.FrecuenciaBajada;
                    satelite.Transponders[targetIndex].FrecuenciaSubida = transponder.FrecuenciaSubida;
                    satelite.Transponders[targetIndex].FrecuenciaBandaL = transponder.FrecuenciaBandaL;
                    satelite.Transponders[targetIndex].numeroXPSatelite = transponder.numeroXPSatelite;
                    satelite.Transponders[targetIndex].color = transponder.color;
                }
            }

            return Json(ModelState.ToDataSourceResult());
        }

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Transponders_Delete([DataSourceRequest] DataSourceRequest request, Transponder transponder, int IdSatelite)
        {
            if (transponder != null && ModelState.IsValid)
            {
                var satelite = listaSatelites.Single(o => o.SateliteId == IdSatelite);
                var transponderTarget = satelite.Transponders.Single(o => o.TransponderID == transponder.TransponderID);

                if (transponderTarget != null)
                {
                    satelite.Transponders.Remove(transponderTarget);
                }
            }

            return Json(ModelState.ToDataSourceResult());
        }



endregion Trandsponder

region Satelites

        public static List<Transponder> listaTransponders = (List<Transponder>)Builder<Transponder>.CreateListOfSize(6).All().Build();


        public static List<Satelite> listaSatelites = (List<Satelite>) Builder<Satelite>
                                    .CreateListOfSize(13)
                                    .All()
                                    .With(x => x.Transponders = listaTransponders)
                                    .Build();

        public ActionResult Satelites() 
        {
            ViewBag.Message = "Satélites";
            return View(GetSatelites());
        }

        [HttpPost]
        public ActionResult Satelites_Read([DataSourceRequest] DataSourceRequest request)
        {
            return Json(listaSatelites.ToDataSourceResult(request), JsonRequestBehavior.AllowGet);
        }

        private static IEnumerable<Satelite> GetSatelites()
        {
            return listaSatelites;
        }

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Satelites_Update([DataSourceRequest] DataSourceRequest request, Satelite satelite)
        {
            if (satelite != null && ModelState.IsValid)
            {
               // var satelites = (List<Satelite>)GetSatelites();
                var target = listaSatelites.Where(o => o.SateliteId == satelite.SateliteId).FirstOrDefault();

                if (target != null)
                {
                    int targetIndex = listaSatelites.IndexOf(target);
                    listaSatelites[targetIndex].SateliteId = satelite.SateliteId;
                    listaSatelites[targetIndex].Denominacion = satelite.Denominacion;
                    listaSatelites[targetIndex].Comentario = satelite.Comentario;
                }
            }

            return Json(ModelState.ToDataSourceResult());
        }

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Satelites_Delete([DataSourceRequest] DataSourceRequest request, Satelite satelite)
        {
            if (satelite != null && ModelState.IsValid)
            {
                // var satelites = (List<Satelite>)GetSatelites();
                var target = listaSatelites.Where(o => o.SateliteId == satelite.SateliteId).FirstOrDefault();

                if (target != null)
                {
                    listaSatelites.Remove(target);
                }
            }

            return Json(ModelState.ToDataSourceResult());
        }

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Satelites_Create([DataSourceRequest] DataSourceRequest request, Satelite satelite)
        {
            if (satelite != null && ModelState.IsValid)
            {
                satelite.SateliteId = listaSatelites.Max(z => z.SateliteId) + 1;
                listaSatelites.Add(satelite);
            }
            return Json(new[] { satelite }.ToDataSourceResult(request, ModelState));
        }

        endregion //satelites

卫星类和转发器类

 public class Satelite
    {
        public int SateliteId { get; set; }
        [Required]
        public string Denominacion { get; set; }
        [Required]
        public string Comentario { get; set; }
        public double UbicacionOrbital { get; set; }
        public List<Transponder> Transponders { get; set; } 

    }
    public class Transponder
    {
        [Required]    
        public int TransponderID { get; set; }
        public Enumeradores.Polaridad Polaridad { get; set; }
        public Enumeradores.Haz Haz { get; set; }
        [Required]
        public string UsuarioAsignado { get; set; }
        public string NumeroTranslacionIRD { get; set; }
        public string Comentario { get; set; }
        public int FrecuenciaBajada { get; set; }
        public int FrecuenciaSubida { get; set; }
        public int FrecuenciaBandaL { get; set; }
        public int numeroXPSatelite { get; set; }
        [UIHint("Color")]
        public string color { get; set; }
    }

任何帮助将不胜感激,并在此先感谢。

PS:对不起我的英语水平不好

4

1 回答 1

1

我建议在 KendoUI CodeLibrary 中检查以下示例,该示例演示了这种行为:

于 2013-11-01T11:20:37.447 回答