【问题标题】:How to display Validation error message in controller using Kendo grid?如何使用 Kendo 网格在控制器中显示验证错误消息?
【发布时间】:2020-05-11 11:39:31
【问题描述】:

我通过以下代码使剑道网格中的字段可编辑:

Html.Kendo().Grid(Model.lstResend)
  .Name("ResendFlowGride")
      .Columns(
             column =>
             {
                    column.Bound(e => e.FLOW_ID).Title("Id").Hidden(true);
                    column.Bound(e => e.GROUP_ID).Title("Group Id").Hidden(true);
                    column.Bound(e => e.GROUP_NAME).Title("Group Name");
                    column.Bound(e => e.ITEM_ID).Title("Item Id").Hidden(true);
                    column.Bound(e => e.ITEM_NAME).Title("Item Name");
                    column.Bound(e => e.ITEM_VALUE).Title("Item Value");
                        //  column.Bound(e => e.ITEM_VALUE).Ed

                 }
            )
         .Editable(editable => editable.Mode(GridEditMode.InCell))
         .DataSource(datasource => datasource.Ajax()

         .Model(model =>
         {
             model.Id(p => p.ITEM_ID);
             model.Field(p => p.ITEM_ID).Editable(false);
             model.Field(p => p.GROUP_ID).Editable(false);
             model.Field(p => p.GROUP_NAME).Editable(false);
             model.Field(p => p.ITEM_NAME).Editable(false);
             model.Field(p => p.ITEM_VALUE).Editable(true);
         })
         .Update(update => update.Action("Update", "Registration"))
         )

我有下面的控制器:

public ActionResult Update([DataSourceRequest]DataSourceRequest request, ResendFlow txns)
{
    try
    {
        if (txns != null && ModelState.IsValid)
        {
            var regDetailsToUpdate = _repository.Context.REG_REGISTRATION_MST_T.Where(x => x.REGISTRATION_ID == txns.Reg_Id).FirstOrDefault();
            _repository.Update(regDetailsToUpdate, regDetailsToUpdate.REGISTRATION_ID);
            if (txns.ITEM_NAME == "Standard Settlement Configuration Id")
            {
                regDetailsToUpdate.STANDARD_SETTLEMENT_CONFIGURATION_ID = txns.ITEM_VALUE;
                if (String.IsNullOrEmpty(txns.ITEM_VALUE))
                {
                    //display error msg -> item valu
                    return Json(new { success = false, validinput = false, message = "Please enter the Standard Settlement Configuration Id" }, JsonRequestBehavior.AllowGet);
                }
                else
                {
                    int i = 0;
                    string s = txns.ITEM_VALUE;//"0393"
                    bool result = int.TryParse(s, out i);//0393

                    if (!result == true)
                    {
                        ModelState.AddModelError(txns.ITEM_VALUE, "Not Valid SSC");
                    }
                }
            }

            _repository.Save();
            return Json(new { success = true, validinput = true, message = "Updated details Successfully" }, JsonRequestBehavior.AllowGet);
        }
    }
    catch (Exception e)
    {
    }
    return Json(ModelState.ToDataSourceResult());
}

如果用户输入字符而不是数字,我想显示验证消息。如何在控制器中使用剑道显示验证错误消息?

【问题讨论】:

  • 我已经更新了我的答案并添加了一个示例 javascript 函数来帮助你

标签: c# model-view-controller kendo-ui


【解决方案1】:

尝试替换

return Json(ModelState.ToDataSourceResult());

与

return Json(new []{ txns }.ToDataSourceResult(request, ModelState), JsonRequestBehaviour.AllowGet);

您希望在数据源请求中将ModelState 和request 传回。

在网格本身中,您希望对 DataSource 的 Error 方法有一个引用

.DataSource(datasource => datasource
    .Ajax()
    .Events(e => e
        .Error("Grid_Error")    // Can be named anything but it's usually best to add the suffix '_Error'
    )    

现在您只需要一个调用 this 并打印错误的 javascript 函数,例如:

function Grid_Error(e){
    if(e.errors){
        var message = "There are some errors:\n";

        // Loop through the errors you defined in the controller
        $.each(e.errors, function(key, value){
            if('errors' in value)
            {
                $.each(value.errors, function(){
                    message += this. '\n';
                })
            }
        })

        alert(message);

        // Here you can simply cancel the changes using the datasource, reverting the grid back to its original state.
    }
}

注意:如果这个网格是部分的,你可能需要把这个 javascript 函数放在上一级,在父级的 cshtml 中

【讨论】:

    猜你喜欢
    • 2013-07-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多