grid kendo ui edit create delete

时间:2013-08-05 12:46:40

标签: user-interface grid kendo-ui

我有页面mvc cshtml

它包含一个begin beginform:

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

在这个表单里面我把网格kendo ui放在我要使用的这个kendo网格中

在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"))

    )
)

1 个答案:

答案 0 :(得分:0)

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

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和teh返回类型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));
    }
}