2015-04-28 150 views
1

对于特殊的网格,我需要为用户提供更新和删除行的可能性。在ASP.NET MVC Kendo UI Grid中删除/更新操作的问题

我的代码是基本相同的位置:http://demos.telerik.com/aspnet-mvc/grid/editing-popup 但是,更新和删除操作不work.No异常推出,但该行仍然是相同的,当我刷新网格:

这里是我的代码:

@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方法效果很好!

回答

2

Update您必须返回新备注(nf)而不是旧备注(note)。

[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)); 
} 
+0

是的,那是第一个错误。不幸的是,当我尝试编辑一行时,它被删除 – FieryA

+0

删除现在可以使用。有只是更新部分不工作(删除行而不是更新) – FieryA

+1

@FieryA很高兴听到其中一个问题得到解决:)。可以有更多的笔记具有相同的'Id'?在删除你有一个'列表'和更新只是一个元素。 – adricadar

相关问题