简体   繁体   English

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

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

When i add a new record or modify an existing row, I want to validate the new data in the Action Method. 当我添加新记录或修改现有行时,我想在操作方法中验证新数据。 If the new value entered is not within a certain range 1 - 10 ( or if an existing value is modified to be outside the valid range), I don't want the insert/update to be successful. 如果输入的新值不在1到10的特定范围内(或者将现有值修改为超出有效范围),则我不希望插入/更新成功。

I tried the following: 我尝试了以下方法:

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

However, when the method returns, a new row with the invalid Price data is added to the grid. 但是,当方法返回时,带有无效价格数据的新行将添加到网格中。

What am I missing? 我想念什么? How can I fix the return statement to handle this case? 如何修复return语句以处理这种情况?

This Kendo UI Blog post might give you some ideas on how to handle this: http://www.kendoui.com/blogs/teamblog/posts/13-08-29/handling-server-side-validation-errors-in-your-kendo-ui-grid.aspx 这份Kendo UI博客文章可能会给您一些有关如何处理此问题的想法: http : //www.kendoui.c​​om/blogs/teamblog/posts/13-08-29/handling-server-side-validation-errors-in-您的kendo-ui-grid.aspx

Basically, you can add errors to the ModelState error collection on the server: 基本上,您可以将错误添加到服务器上的ModelState错误集合中:

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

Then the Kendo ToDataSourceResult() function will put those ModelState errors into an errors collection in the JSON returned to the client. 然后,Kendo ToDataSourceResult()函数会将那些ModelState错误放入返回给客户端的JSON中的errors集合中。

On the client side, the DataSource's error function will be called when there are messages in the server response's error collection. 在客户端,当服务器响应的错误集合中有消息时,将调用DataSource的错误函数。

Then you can handle the errors in the DataSource error function. 然后,您可以处理DataSource错误函数中的错误。

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

Well, maybe you could go with the "edit" event of Kendo Grid. 好吧,也许您可​​以参加Kendo Grid的“ edit”事件。 See docs here: http://docs.kendoui.com/api/web/grid#events-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