How to display Validation error message in controller using Kendo grid?

独自空忆成欢 提交于 2020-02-05 04:25:06

问题


I made the fields in the kendo grid editable by the following code:

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"))
         )

I have the Below Controller:

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

I wanted to display Validation Message if the user enters characters instead of number.How to display Validation error message using kendo in Controller?


回答1:


Try replacing

return Json(ModelState.ToDataSourceResult());

with

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

You want to be passing the ModelState and the request back in the data source request.

In the grid itself you want to have a ref to the Error method of DataSource

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

Now all you need is a javascript function that is called this and prints the errors such as:

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.
    }
}

NOTE: If this grid is in a partial, you may need to put this javascript function a level up, in the parent's cshtml



来源:https://stackoverflow.com/questions/59908385/how-to-display-validation-error-message-in-controller-using-kendo-grid

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!