【问题标题】:grid kendo ui edit create delete网格剑道 ui 编辑创建删除
【发布时间】:2013-08-05 12:46:39
【问题描述】:

我有页面 mvc cshtml

它包含一个begin beginform:

@using (Html.BeginForm("ValiderLeChangement", "FichePersonnel", FormMethod.Post , new { id = "FormValider" }  ))
{
}

在这个表格中,我将网格剑道 ui 放在了我想使用的剑道网格中

在 Kendo UI DEMO (http://demos.kendoui.com/web/grid/editing-inline.html) 中创建添加和删除示例

我对创建、删除或编辑的操作有问题,但服务器上不会触发

我的代码是:

@(Html.Kendo().Grid(Model.ListeContact)
    .Name("Grid")
    .Editable(editable => editable.Mode(GridEditMode.InLine))
    .ToolBar(toolbar => toolbar.Create())
    .Columns(columns =>
    {
        columns.Bound(p => p.Nom).Title("Nom").Width(20);
        columns.Bound(p => p.Prenom).Title("Prenom").Width(20);

        //columns.Bound(p => p.Lien.Libelle).Title("Lien").Width(20).ClientTemplate(????? );
        columns.Bound(p => p.Lien.Libelle).Title("Lien").Width(20);

        columns.Bound(p => p.Tel).Title("Telephone").Width(20);

        columns.Command(command => { command.Edit(); }).Width(30);
        columns.Command(command => { command.Destroy(); }).Width(30);
      /////////  columns.Bound(p => p.IdContact).ClientTemplate("#= Delete(data) #").Title("Supprimer").Width(5);
    })
    .Sortable()
    .DataSource(dataSource => dataSource
    .Ajax()
    .Batch(true)
    .ServerOperation(true)
    .Events(events => events.Error("error_handler"))
    .Read(Read => Read.Action("ListeContact_Read", "FichePersonnel"))
        .Model(model => model.Id(p => p.IdContact))
        .Create(create => create.Action("EditingInline_Create", "FichePersonnel"))
        .Update(update => update.Action("EditingInline_Update", "FichePersonnel"))
        .Destroy(delete => delete.Action("EditingInline_Destroy", "FichePersonnel"))

    )
)

【问题讨论】:

    标签: user-interface grid kendo-ui


    【解决方案1】:

    您是如何实现控制器操作方法的?它是否处理请求并正确返回?应该是这样的:

    public ActionResult EditingInline_Create([DataSourceRequest] DataSourceRequest request, ProductViewModel product)
        {
            if (product != null && ModelState.IsValid)
            {         
                SessionProductRepository.Insert(product);                                 
            }
    
            return Json(new [] { product }.ToDataSourceResult(request, ModelState));
        }
    

    注意第一个参数 DataSourceRequest 和返回类型 Json。也许这就是你所缺少的。我还注意到您表示的是 ServerOperation(true)。据我了解,如果您按原样使用 AJAX 绑定,则不需要这样做。

    编辑: 所以对于控制器代码,类似这样的:

        public partial class TextosController : EditorImageBrowserController
    {
        public ActionResult ReadTextos([DataSourceRequest]DataSourceRequest request)
        {
            CostSimulatorModel modelo = new CostSimulatorModel(new Uri(@"http://localhost:53212/CostSimulatorModelService.svc/"));
    
            IQueryable<Texto> textos = modelo.Textos;
            DataSourceResult resultado = textos.ToDataSourceResult(request);
            ViewData["Textos"] = textos;
            return Json(resultado, JsonRequestBehavior.AllowGet);
        }
        public ActionResult CreateTexto([DataSourceRequest]DataSourceRequest request, Texto texto)
        {
            if (ModelState.IsValid)
            {
                CostSimulatorModel modelo = new CostSimulatorModel(new Uri(@"http://localhost:53212/CostSimulatorModelService.svc/"));
    
                // Create a new Product entity and set its properties from the posted ProductViewModel
                Texto entity = new Texto
                {
                    TextoID = texto.TextoID,
                    Titulo = texto.Titulo,
                    Corpo = texto.Corpo,
                    IsPrivado = texto.IsPrivado,
                    TipoTextoID = texto.TiposTexto != null ? texto.TiposTexto.TipoTextoID : texto.TipoTextoID,
                    TiposTexto = texto.TiposTexto
                };
                modelo.AddToTextos(entity);
                // Insert the entity in the database
                modelo.SaveChanges();
                // Get the ProductID generated by the database
                texto.TextoID = entity.TextoID;
                return Json(new[] { entity }.ToDataSourceResult(request, ModelState));
            }
            // Return the inserted product. The grid needs the generated ProductID. Also return any validation errors.
            return Json(new[] { texto }.ToDataSourceResult(request, ModelState));
        }
    
        public ActionResult UpdateTexto([DataSourceRequest]DataSourceRequest request, Texto texto)
        {
            if (ModelState.IsValid)
            {
                CostSimulatorModel modelo = new CostSimulatorModel(new Uri(@"http://localhost:53212/CostSimulatorModelService.svc/"));
    
                // Create a new Product entity and set its properties from the posted ProductViewModel
                var entity = new Texto
                {
                    TextoID = texto.TextoID,
                    Titulo = texto.Titulo,
                    Corpo = texto.Corpo,
                    IsPrivado = texto.IsPrivado,
                    TipoTextoID = texto.TiposTexto != null ? texto.TiposTexto.TipoTextoID : texto.TipoTextoID,
                    TiposTexto = texto.TiposTexto
                };
                // Attach the entity
                modelo.AttachTo("Textos", entity);
                modelo.UpdateObject(entity);
                // Update the entity in the database
                modelo.SaveChanges();
                return Json(new[] { entity }.ToDataSourceResult(request, ModelState));
            }
            // Return the updated product. Also return any validation errors.
            return Json(new[] { texto }.ToDataSourceResult(request, ModelState));
        }
    
        public ActionResult DestroyTexto([DataSourceRequest]DataSourceRequest request, Texto texto)
        {
            if (ModelState.IsValid)
            {
                CostSimulatorModel modelo = new CostSimulatorModel(new Uri(@"http://localhost:53212/CostSimulatorModelService.svc/"));
    
                // Create a new Product entity and set its properties from the posted ProductViewModel
                var entity = new Texto
                {
                    TextoID = texto.TextoID
                };
                // Attach the entity
                modelo.AttachTo("Textos", entity);
                // Delete the entity
                modelo.DeleteObject(entity);
    
                // Delete the entity in the database
                modelo.SaveChanges();
                return Json(new[] { entity }.ToDataSourceResult(request, ModelState));
            }
            // Return the removed product. Also return any validation errors.
            return Json(new[] { texto }.ToDataSourceResult(request, ModelState));
        }
    }
    

    【讨论】:

    • 谢谢我写的代码和 ewample 和你的方法一样
    • 现在可以工作,但是在创建或更新我的网格 kendo 之后我有问题 如果我尝试创建或更新我有错误,例如我要更新的对象的 ID 为 nul
    • 所以如果我刷新 (F5) 一切正常 ==> 如何强制从控制器刷新页面
    • 我还没有完成我的操作创建更新是 ajax 谢谢
    • 你是如何在控制器上实现你的动作的?你能展示一下这段代码吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-22
    • 2023-04-03
    • 1970-01-01
    • 2014-04-29
    • 2012-02-14
    相关资源
    最近更新 更多