如何使用Kendo网格在控制器中显示验证错误消息?

凯坦·坎布利

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

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());
}

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

詹姆士

尝试更换

return Json(ModelState.ToDataSourceResult());

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

您要在数据源请求中传递ModelStaterequest返回。

在网格本身中,您需要引用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'
    )    

现在,您需要的是一个名为javascript的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中

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

在表单数组 angular 中验证和显示特定表单控制器的错误消息

如何通过角度在控制器上显示错误消息

如何在 Rails 测试中显示控制器错误消息?

如何在控制器中定位错误消息

需要在视图上显示错误消息,该错误消息已在控制器中设置

如何从Angular控制器调用Kendo网格上的refresh()?

如何使用i18n在控制器中返回自定义的休眠验证消息?

使用Kendo UI验证程序如何隐藏默认验证错误消息并仅在工具提示上显示消息

如何在控制器中捕获错误或验证棱角材料日期选择器?

如何显示从HttpResponseErrInterceptor到特定控制器的响应消息

如何在MVC 4控制器中显示警报消息?

用Django编写验证器:如何显示验证错误消息?

如何显示字段错误和控制器在jquery ajax中有条件发送的错误消息

验证后如何在Laravel控制器中捕获并返回错误?

如何在RESTful Spring MVC控制器中处理验证错误和异常?

如何通过显示JSP文件中的错误来处理控制器中的异常?

在Swift中的UITableViewController中搜索显示控制器错误

将数据从控制器传递到 Laravel 中查看 - 显示错误消息未定义变量

如何在控制器 Laravel 中获取翻译消息?

覆盖控制器后如何在ActiveAdmin表单中显示错误

如果控制器发生故障,如何在View中显示错误?ASP.NET MVC

如何使用按钮显示“搜索显示控制器”

Laravel功能控制器验证错误

如何使用模型和控制器验证cakephp中的表单字段

如何敬酒验证错误消息显示未选择微调器中的任何项目?

Spring MVC验证器中未显示错误消息

如何在ExtJS网格验证中显示错误?

spring 验证器不使用属性文件显示错误消息

如何在 Odoo 中显示带有来自控制器的警告消息的对话框?