KendoUI Grid ASP.NET MVC版本数值验证实现方式咨询
Great question! When working with the ASP.NET MVC version of the Kendo UI Grid, you have two straightforward ways to implement those minimum value validation rules—either using data annotations on your model or configuring validation directly in the Grid's column definitions. Let's break both down:
Using Data Annotations on Your Model
This is the most declarative approach, and it enforces validation both client-side (in the Grid) and server-side (when saving data). Just add [Range] attributes to your model properties to define the minimum values:
public class Product { // Other model properties (e.g., ProductID, ProductName) [Range(1, double.MaxValue, ErrorMessage = "Price must be at least $1.00")] public decimal Price { get; set; } [Range(0, int.MaxValue, ErrorMessage = "Units in stock cannot be negative")] public int UnitsInStock { get; set; } }
When you bind your Kendo Grid to this model, it will automatically detect these attributes and apply the validation rules during editing—no extra Grid configuration needed for validation logic here.
Configuring Validation Directly in the Grid Columns
If you prefer to define validation rules directly in your Grid setup (useful for dynamic scenarios or if you don't want to modify the model), you can use the .Validation() method on each column to set minimum values:
@(Html.Kendo().Grid<Product>() .Name("grid") .Columns(columns => { // Add other columns as needed columns.Bound(p => p.Price) .Title("Price") .Validation(v => v.Min(1).Message("Price must be at least $1.00")); columns.Bound(p => p.UnitsInStock) .Title("Units In Stock") .Validation(v => v.Min(0).Message("Units in stock cannot be negative")); }) .Editable(editable => editable.Mode(GridEditMode.InLine)) // Or your preferred edit mode (Popup/InCell) // Add other Grid configurations (data source, sorting, etc.) )
Both methods will mirror the behavior of the HTML5/JavaScript version you referenced: users will see error messages if they enter invalid values, and they won't be able to save until the values meet the minimum thresholds. For complete data integrity, always pair client-side validation with server-side checks (the data annotation approach already handles this automatically!).
内容的提问来源于stack exchange,提问作者Christoph Biegner

