繁体   English   中英

ASP.NET MVC Kendo UI网格中的删除/更新操作问题

[英]Issues with Delete/Update Operations in an ASP.NET MVC Kendo UI Grid

对于特殊的网格,我需要为用户提供更新和删除行的可能性。

我的代码与这里的代码基本相同: http ://demos.telerik.com/aspnet-mvc/grid/editing-popup但是更新和删除操作不起作用。没有例外被启动,但行仍然相同当我刷新网格时:

这是我的代码:

@using Kendo.Mvc.UI
@model DevelopmentNotesProject.Models.NoteJoinLanguage

@{
    ViewBag.Title = "Index";
}


<script type="text/javascript">
    function formatter(value) {
        return value.substring(0, 50);
    }
</script>


<section id="listing">
    <h2>My Notes</h2>

        @(Html.Kendo().Grid<DevelopmentNotesProject.Models.NoteJoinLanguage>()
        .Name("grid")
        .Columns(columns =>
        {
            columns.Bound(c => c.Title).Width(400).ClientTemplate(string.Format("{0}...", "#= formatter(Title) #"));
            columns.Bound(c => c.Text).Width(400).ClientTemplate(string.Format("{0}...", "#= formatter(Text) #"));
            columns.Bound(c => c.lang).Title("Language");
            columns.Command(command => { command.Edit(); command.Destroy(); }).Width(160);
        })
        .HtmlAttributes(new { style = "height: 380px;" })
        .Scrollable()
        .Groupable()
        .Sortable()
        .Pageable(pageable => pageable
            .Refresh(true)
            .PageSizes(true)
            .ButtonCount(5))
         .DataSource(dataSource => dataSource // Configure the grid data source
                  .Ajax() // Specify that ajax binding is used
                  .Read(read => read.Action("Notes_Read", "MyNotes")) // Set the action method which will return the data in JSON format
                  .Destroy(update => update.Action("Notes_Destroy", "MyNotes"))
                  .Update(update => update.Action("Notes_Update", "MyNotes"))
                  .Model(model => model.Id(p => p.id))
               )
         .Selectable()
        )



  </section>

@Html.ActionLink("Add a new note", "Add", "MyNotes")

 @section Scripts {
        @Scripts.Render("~/bundles/jqueryval")
    }

最后是我的控制器:

   public ActionResult Notes_Read([DataSourceRequest]DataSourceRequest request)
        {
            var dbo = new UsersContext();
            var notes = (from a in dbo.Note
                         join b in dbo.Language on a.languageId equals b.id
                         where a.userId == WebSecurity.CurrentUserId
                         select new { a.id, a.Text, a.Title, b.lang }).ToList();

            DataSourceResult result = notes.ToDataSourceResult(request);
            return Json(result);
        }

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Notes_Destroy([DataSourceRequest]DataSourceRequest request, NoteForm note)
        {
            var dbo = new UsersContext();
            var results = (from row in dbo.Note
                          where row.id == note.id
                          select row).ToList();

            foreach (var item in results)
            {
                dbo.Note.Remove(item);
            }
            return Json(new[] { note }.ToDataSourceResult(request, ModelState));
        }

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Notes_Update([DataSourceRequest]DataSourceRequest request, NoteForm note)
        {
            var dbo = new UsersContext();
            NoteForm nf = (from row in dbo.Note
                          where row.id == note.id
                          select row).First();

            nf.languageId = note.languageId;
            nf.Text = note.Text;
            nf.Title = note.Title;
            dbo.SaveChanges();
            return Json(new[] { note }.ToDataSourceResult(request, ModelState));

        }

预先感谢您的帮助PS:Notes_read方法效果很好!

在“ Update操作”中,您必须返回新的注释( nf )而不是旧的notenote )。

[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Notes_Update([DataSourceRequest]DataSourceRequest request, NoteForm note)
{
    var dbo = new UsersContext();
    NoteForm nf = (from row in dbo.Note
                  where row.id == note.id
                  select row).First();

    nf.languageId = note.languageId;
    nf.Text = note.Text;
    nf.Title = note.Title;
    dbo.SaveChanges();
    return Json(new[] { nf }.ToDataSourceResult(request, ModelState));
}

在“ Destroy操作”中,您必须在删除项目后调用dbo.SaveChanges()

[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Notes_Destroy([DataSourceRequest]DataSourceRequest request, NoteForm note)
{
    var dbo = new UsersContext();
    var results = (from row in dbo.Note
                  where row.id == note.id
                  select row).ToList();

    foreach (var item in results)
    {
        dbo.Note.Remove(item);
    }
    dbo.SaveChanges();

    return Json(new[] { note }.ToDataSourceResult(request, ModelState));
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM