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方法效果很好!
是的,那是第一個錯誤。不幸的是,當我嘗試編輯一行時,它被刪除 – FieryA
刪除現在可以使用。有隻是更新部分不工作(刪除行而不是更新) – FieryA
@FieryA很高興聽到其中一個問題得到解決:)。可以有更多的筆記具有相同的'Id'?在刪除你有一個'列表'和更新只是一個元素。 – adricadar