2013-10-31 89 views
1

我目前正在使用帶有Razor引擎的MVC4 KendoUI項目C#。 經過在網上搜索了很多,我找不到方法來實現這一點:我有一個基於我的模型的Kendo網格,它是一個「衛星」對象的集合,有一些屬性,其屬性之一是「轉發器」對象的列表。Kendo UI - SubGrid裏面彈出編輯器網格

我有我的「衛星」觀點,即由可編輯劍道電網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:對不起,我可憐的英語水平

回答

1

我會建議您在KendoUI CodeLibrary下面的例子演示這種行爲:

+0

不好意思,那個沒有按爲我工作,我試圖做的例子,我得到了「未捕獲的ReferenceError:劍未定義」。 在這部分代碼中: var territoriesTemplate = kendo.template($(「#territoriesTemplate」)。html(),{useWithBlock:false}); –

+0

@JeroFranzani你可能錯過了一些東西,它實際上爲我工作。 – Abdul

相關問題