繁体   English   中英

在ASP.NET MVC中插入Kendo Grid之前先验证数据

[英]Validate data before inserting to Kendo Grid in ASP.NET MVC

当我添加新记录或修改现有行时,我想在操作方法中验证新数据。 如果输入的新值不在1到10的特定范围内(或者将现有值修改为超出有效范围),则我不希望插入/更新成功。

我尝试了以下方法:

[AcceptVerbs(HttpVerbs.Post)]
public ActionResult EditingInline_Create([DataSourceRequest] DataSourceRequest request,   ProductViewModel product)
    {
         if (product != null && ModelState.IsValid)
         {    
              if (product.Price > 1 && product.Price < 10)
              {
                   SessionProductRepository.Insert(product); 
              }                               
         }

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

但是,当方法返回时,带有无效价格数据的新行将添加到网格中。

我想念什么? 如何修复return语句以处理这种情况?

这份Kendo UI博客文章可能会给您一些有关如何处理此问题的想法: http : //www.kendoui.c​​om/blogs/teamblog/posts/13-08-29/handling-server-side-validation-errors-in-您的kendo-ui-grid.aspx

基本上,您可以将错误添加到服务器上的ModelState错误集合中:

ModelState.AddModelError("SomsField", "Some error message.");

然后,Kendo ToDataSourceResult()函数会将那些ModelState错误放入返回给客户端的JSON中的errors集合中。

在客户端,当服务器响应的错误集合中有消息时,将调用DataSource的错误函数。

然后,您可以处理DataSource错误函数中的错误。

您可以调用grid.cancelChanges()来防止数据源中发生任何未决的更改,如网址http://docs.kendoui.c​​om/api/web/grid#methods-cancelChanges中所述

好吧,也许您可​​以参加Kendo Grid的“ edit”事件。 在此处查看文档: http : //docs.kendoui.c​​om/api/web/grid#events-edit

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM