简体   繁体   English

ASP.NET MVC。 是否可以使用MVC防呆验证进行阵列验证?

[英]ASP.NET MVC. Is it possible to use MVC Foolproof Validation for array validation?

Eg I have the following model: 例如,我有以下模型:

public class MyModel {
public Line[] Lines{get;set;}
}

public class Line {
public int Quantity{get;set;}
public int MaxQuantity{get;set;}
}

The "Quantity" field should be less than "MaxQuantity" so I tried to use "LessThanOrEqualTo" validation attribute: “数量”字段应小于“最大数量”,因此我尝试使用“ LessThanOrEqualTo”验证属性:

public class Line {
[LessThanOrEqualTo("MaxQuantity")]
public int Quantity{get;set;}
public int MaxQuantity{get;set;}
}

But it does not work on client side because of wrong dependent property name I believe: 但这在客户端不起作用,因为我相信错误的依赖属性名称:

<input type="number" value="1" name="Lines[0].Quantity" data-val-is-passonnull="False" data-val-is-operator="LessThanOrEqualTo" data-val-is-dependentproperty="MaxQuantity" data-val-is="Quantity must be less than or equal to MaxQuantity." data-val="true">
<input type="hidden" value="1" name="Lines[0].MaxQuantity">

You can see that " data-val-is-dependentproperty="MaxQuantity" " but real input name is " Lines[0].MaxQuantity" " 您可以看到“ data-val-is-dependentproperty="MaxQuantity" “,但实际输入名称为” Lines[0].MaxQuantity"

Is there any way to fix this? 有没有什么办法解决这一问题?

Have you included mvcfoolproof.unobtrusive.min.js in your view or your master page? 您是否在视图或母版页中包含了mvcfoolproof.unobtrusive.min.js? Have you checked that the script is loaded before your code is using it? 您是否在代码使用之前检查脚本是否已加载?

With my Foolproof Nuget installation, it was placed in "Client Scripts" folder from Foolproof Nuget installation. 在我的Foolproof Nuget安装中,它被放置在Foolproof Nuget安装的“客户端脚本”文件夹中。

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

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