如何在ActionResult的视图中打开Kendo窗口

时间:2015-03-05 08:48:43

标签: c# asp.net-mvc-4 razor kendo-ui

当用户从kendo网格添加时,我需要检查现有实体。这很好。
如果要添加一个可能的副本,我会坚持在网格顶部显示一个Kendo窗口,并提供合并,添加或取消的选项。

我有一个DivotAdminController,它是View。我添加了一个与DivotCreate Action同名的partialView:

[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Divots_Create([DataSourceRequest] DataSourceRequest request, Divot divot)
{
    using (var context = new DataContext())//Check for Barcode match and ask user what to do if found
    {
        var existingDivotId = context.Divot
            .Include("CategoryLevel1")              
            .Join(context.Containers, ast => ast.ContainerId, cts => cts.Id, (ast, cts) => new { Divot = ast, Container = cts })            
            .Where(a => a.Divot.Barcode == divot.Barcode)
            .OrderByDescending(a => a.Divot.CaptureDate)
            .First().Divot.Id;

        var existingDivot = context.Divot.Where(a => a.Id == existingDivotId).FirstOrDefault();

        if (existingDivot != null)
        {
            return PartialView("Divots_Create", existingDivot);
        }
    }

    if (divot != null) //if not a match save the new entry.
    {
        var containerid = (guid)tempdata["selectcontainerid"];
        divot.Id = Guid.NewGuid();
        divot.CaptureDate = DateTime.Now;
        divot.ModifiedDate = DateTime.Now;
        divot.Username = User.Identity.Name;

        _repository.AddDivot(divot);
    }

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

找到匹配后,return PartialView("Divots_Create", existingDivot);行会执行,但客户端没有任何反应 我想知道是否有一种方法可以将局部视图显示为模态,或者如果找到重复,则将kendo窗口显示为模态?

1 个答案:

答案 0 :(得分:1)

在您的操作中添加模型错误:

ModelState.AddModelError("", "User name already exists");

在错误时为网格分配事件处理程序:

.Events(events => events.Error("handleError"))

通过javascript函数(显示窗口或其他内容)处理客户端错误

    handleError = function (args) {
        if (args.errors) {
            var grid = $("#grid").data("kendoGrid");
            grid.one("dataBinding", function (e) {
                e.preventDefault();
                $.each(args.errors, function (propertyName) {
                   var error = this.errors[0];
                });
            });
        }
    };

有关详细信息,请参阅此Blog post

相关问题