【问题标题】:Kendo Grid Mvc - Cannot be shown custom error message on view pageKendo Grid Mvc - 无法在视图页面上显示自定义错误消息
【发布时间】:2013-11-20 10:04:13
【问题描述】:

我在我的 MVC 项目中使用 Kendo Grid。

我的问题是错误消息没有显示在视图页面上。当我使用开发者工具查看 JSON 数据时,我可以看到正确的错误数据:

{
    "Data": null,
    "Total": 0,
    "AggregateResults": null,
    "Errors": [ "There is record(s) with same name.Please review your new records." ]
}

异常消息已传递给控制器​​中的result 参数。

控制器

catch (Exception exp)
{
    var  result = exp.ToDataSourceResult(request);
    // var  result1 = ModelState.ToDataSourceResult();
    //  ModelState.AddModelError("UpdateAmbulance", exp.Message);
    return Json(result, JsonRequestBehavior.AllowGet);
}

这是查看页面上的剑道网格代码:

<!-- Grid  -->
@(Html.Kendo().Grid<AIS.UI.WebService.Proxy.DSrvAllService.AMBULANCEDEFINITIONS>() //Bind the grid to ViewBag.Products
    .Name("grid")
    // .BindTo(Model)
    .Columns(columns =>
    {
        columns.Bound(product => product.DESCRIPTION)
            .Title("<strong>Ambulance Description</strong>")
            .Width("20%");
            //.ClientTemplate("<strong>#:DESCRIPTION#</>strong");

        columns.Bound(product => product.CODE)
            .Title("<strong>Ambulance Description</strong>")
            .Width("20%");

        columns.Command(commands =>
            {
                commands.Destroy().HtmlAttributes(new { id = "buttondelete", style="display:none" }); 
            })
            .Title("Operations")
            .Width("10%");
    })
    .ToolBar(toolbar =>
    {
        toolbar.Create()
            .HtmlAttributes(new { id="addbutton",style = "font-weight:bold;color:blue" })
            .Text("Add Records"); // The "create" command adds new data items
        toolbar.Save(); // The "save" command saves the changed data items
    })
    // Use in-cell editing mode
    .Editable(editable => editable.Mode(GridEditMode.InCell)) 
    .Pageable(pager => pager
        .PageSizes(true)
        .Input(true)
        .Refresh(true)
        // .Messages(messages => messages.Empty("Veri bulunamadı"))
        // .Messages(messages => messages.First("İlk Sayfa"))
        // .Messages(messages => messages.Last("Son Sayfa"))
    )
    .Sortable() // Enable sorting
    .DataSource(dataSource => dataSource
        .Ajax()
        // .Sort(sort => sort.Add("DESCRIPTION").Ascending())
        .ServerOperation(true)
        .Events(events => events.Error("onError"))
        //.AutoSync(true)            
        .Model(model =>
        {
            model.Id(product => product.ID); 
            model.Field(product => product.ID).Editable(false).DefaultValue(Guid.Empty); 
            model.Field(p => p.DESCRIPTION).Editable(false);
            model.Field(product => product.CODE).Editable(false);
        })
        .Events(events => events.Error("onError"))
        .Create(create => create.Action("AmbulanceCreate", "Administrator")) 
        .Read(read => read.Action("AmbulanceRead", "Administrator"))  
        .Update(update => update.Action("AmbulanceUpdate", "Administrator"))  
        .Destroy(destroy => destroy.Action("AmbulanceDelete", "Administrator"))                                      
    )
)

JS

function onError(e, status) {
    if (e.errors) {
        var message = "The following errors have occurred:\n";
        // var message = "Please correct the records that you enter"
        $.each(e.errors, function (key, value) {
            if (value.errors) {
                message += value.errors.join("\n");
            }
        });

        alert(message);
    }
}

【问题讨论】:

  • 很高兴您找到了解决问题的方法。但是,最好通过实际使用下面的“发布您的答案”部分来回答您自己的问题(即不要用答案编辑您的问题)。这样,它不再被列为未答复。谢谢。
  • 谢谢我一直在寻找这个解决方案

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


【解决方案1】:

解决方案:

要将错误消息从控制器传递到视图,您需要使用 ModelState

if (condition)
{
    throw new Exception("Error msg ");
}
catch (Exception exp)
{
    ModelState.AddModelError("UpdateAmbulance", exp.Message);
    var result = ModelState.ToDataSourceResult();
    return Json(result, JsonRequestBehavior.AllowGet);
}

我在 JavaScript 代码中添加了一行,当出现错误消息时回滚最后一条记录:

function onError(e, status) {
    if (e.errors) {
        var message = "\n";
        // var message = "Please correct the records that you enter"
        $.each(e.errors, function (key, value) {
            if (value.errors) {
                message += value.errors.join("\n");
            }
        });

        alert(message);
        $("#grid").data("kendoGrid").cancelChanges(); // Remove changes when error occurs.
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-03-11
    • 2023-03-14
    • 2013-06-16
    • 1970-01-01
    • 2013-08-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多