更新后,KendoUI网格以编辑模式保留

时间:2013-04-10 13:51:36

标签: kendo-ui kendo-grid kendo-asp.net-mvc

我正在使用ASP.NET MVC Server Wrappers为KendoUI Grid使用AJAX进行绑定(v2013.1.319)并使用内联编辑。当我编辑记录并单击更新时,帖子发生在服务器上并且记录成功保存。我按文档建议返回JSON响应,但KendoUI Grid保持编辑模式。如果单击“取消”,则基础本地数据不会反映更改。如果我刷新,则会正确显示更改。我需要一些帮助来找出更新无效的原因。创建和删除函数工作正常。

<div style="width: 800px">
@(Html.Kendo().Grid<RoleGridModel>()
    .Name("grdRoles")
    .Columns(columns =>
    {
        columns.Bound(r => r.Name).Width(200);
        columns.Bound(r => r.Description).Width(300);
        columns.Command(command => { 
            if (security.CanAdd || security.CanUpdate) command.Edit(); 
            if (security.CanDelete) command.Destroy();
            command.Custom("Manage Access").Click("manageAccess");
        })
        .Width(300);
    })
    .Groupable(grouping => grouping
        .Enabled(false))
    .Events(events => { if (security.CanAdd && !security.CanUpdate) events.DataBound("function() { this.table.find('.k-grid-edit').hide(); }"); })
    .DataSource(dataSource => dataSource
        .Ajax()
        .Model(model => model.Id(r => r.RoleId))
        .Events(events => events.Error("error_handler"))
        .Read(read => read.Action("Index", "Roles", new { area = "Setup" }))
        .Create(create => create.Action("Create", "Roles", new { area = "Setup" }))
        .Update(update => update.Action("Edit", "Roles", new { area = "Setup" }))
        .Destroy(destroy => destroy.Action("Delete", "Roles", new { area = "Setup" }))
        .Sort(sort => sort.Add(r => r.Name).Ascending())
        .PageSize(10))
    .Filterable(filtering => filtering
        .Enabled(true))
    .Editable(editable => editable.Mode(GridEditMode.InLine))
    .Pageable(paging => paging
        .Enabled(true)
        .Info(true)
        .PageSizes(false)
        .Refresh(true))
    .Scrollable(scrolling => scrolling
        .Enabled(false)
        .Height(400)
        .Virtual(false))
    .Sortable(sorting => sorting
        .Enabled(true)
        .AllowUnsort(false)
        .SortMode(GridSortMode.SingleColumn))
    .ToolBar(toolbar => { if (security.CanAdd) toolbar.Create(); })
)

        //
    // POST: /Setup/Roles/Edit

    [HttpPost]
    public ActionResult Edit([DataSourceRequest] DataSourceRequest request, RoleGridModel model)
    {
        if (ModelState.IsValid && model != null)
        {
            try
            {
                Role r = Mapper.Map<RoleGridModel, Role>(model);
                r.AppContext = this.AppContext;
                r.SubscriberId = this.AppContext.SelectedSubscription.SubscriberId;
                r.SubscriptionId = this.AppContext.SelectedSubscription.SubscriptionId;
                r.ModifyDate = DateTime.UtcNow;
                r.IsNew = false;
                r.IsMarkedForDelete = false;
                r.HasChanges = true;
                r.Save();
            }
            catch (Exception ex)
            {
                LogException(ex);
                ModelState.AddModelError("", "Unable to update role. " + ex.Message);
            }
        }

        return Json(ModelState.ToDataSourceResult());
    }

2 个答案:

答案 0 :(得分:5)

显然,jQuery的新版本中有breaking change影响了Kendo Q1 2013版本2013.1.319。在成功更新和销毁请求时,从服务器返回空结果。这会触发错误,因为空结果无效JSON。

此时有2项决议:
1)获取KendoUI的最新内部版本,因​​为它有一个修复 2)返回序列化为JSON的虚拟对象。

return Json(ModelState.IsValid ? new object() : ModelState.ToDataSourceResult());

答案 1 :(得分:-1)

感谢Bill,它对我有用。

我的更新功能正在返回
返回Json(ModelState.ToDataSourceResult()); 当我检查它实际上是虚空。现在我已按照你的建议返回了一个对象,它运行正常。

这是有效的 返回Json(ModelState.IsValid?new object():ModelState.ToDataSourceResult());

相关问题