C# MVC 3如何验证整个ViewModel?
我试图验证一个模型,其中规则并不总是相同的,并且依赖于模型中的其他属性。做这件事最好的方法是什么?示例如下: 假设例1 在MVC3中使用MVVM模式。我的(假设的)视图模型如下所示:C# MVC 3如何验证整个ViewModel?,c#,asp.net-mvc-3,validation,mvvm,model,C#,Asp.net Mvc 3,Validation,Mvvm,Model,我试图验证一个模型,其中规则并不总是相同的,并且依赖于模型中的其他属性。做这件事最好的方法是什么?示例如下: 假设例1 在MVC3中使用MVVM模式。我的(假设的)视图模型如下所示: public string OrderType { get; set; } public string Requestor { get; set; } public int NumberOfPeanuts { get; set; } public int NumberOfJellyBeans { get; set;
public string OrderType { get; set; }
public string Requestor { get; set; }
public int NumberOfPeanuts { get; set; }
public int NumberOfJellyBeans { get; set; }
public int NumberOfAlmonds { get; set; }
@Html.EditorFor(model => model.OrderType )
@Html.EditorFor(model => model.Requestor )
@Html.EditorFor(model => model.NumberOfPeanuts )
@Html.EditorFor(model => model.NumberOfJellyBeans )
@Html.EditorFor(model => model.NumberOfAlmonds )
我的观点基本上是这样的:
public string OrderType { get; set; }
public string Requestor { get; set; }
public int NumberOfPeanuts { get; set; }
public int NumberOfJellyBeans { get; set; }
public int NumberOfAlmonds { get; set; }
@Html.EditorFor(model => model.OrderType )
@Html.EditorFor(model => model.Requestor )
@Html.EditorFor(model => model.NumberOfPeanuts )
@Html.EditorFor(model => model.NumberOfJellyBeans )
@Html.EditorFor(model => model.NumberOfAlmonds )
如何实现将返回以下规则的“Html.ValidationMessageFor”结果的验证:
如果OrderType=“Peanuts”,则NumberOfPeanuts必须大于0,NumberOfJellyBeans和NumberOfAlmonds必须为null或0,否则显示“这是仅限花生的订单”
如果OrderType=“Sample”,则NumberOfPeanuts+NumberOfJellyBeans+NumberOfAlmonds必须小于30,否则显示验证消息“样本总量不够高”
等等。。。等等。您可以使用自己的自定义验证属性扩展
ValidationAttribute
public class CustomAttribute : ValidationAttribute
{
protected override ValidationResult IsValid(object value, ValidationContext validationContext)
{
// validation logic
}
}
我将详细介绍Sam的答案,以使用多个模型属性来验证:
public class PeanutOrderAttribute : ValidationAttribute, IClientValidatable
{
private readonly string _peanutsProperty;
private readonly string _orderTypeProperty;
public PeanutOrderAttribute(string peanutsPropertyName, string orderTypePropertyName)
{
_peanutsProperty = peanutsPropertyName;
_orderTypeProperty = orderTypePropertyName;
}
protected override ValidationResult IsValid(object value, ValidationContext validationContext)
{
// get target property and value
var peanutInfo = validationContext.ObjectType.GetProperty(this._peanutsProperty);
var peanutValue = peanutInfo.GetValue(validationContext.ObjectInstance, null);
// get compare property and value
var ordertypeInfo = validationContext.ObjectType.GetProperty(this._orderTypeProperty);
var ordertypeValue = ordertypeInfo.GetValue(validationContext.ObjectInstance, null);
// if validation does not pass
if (ordertypeValue == "Peanuts" && peanutValue < 1){
return new ValidationResult("Error Message");
}
// else return success
return ValidationResult.Success;
}
public IEnumerable<ModelClientValidationRule> GetClientValidationRules(ModelMetadata metadata, ControllerContext context)
{
var rule = new ModelClientValidationRule
{
ErrorMessage = this.ErrorMessageString,
ValidationType = "PeanutOrder"
};
rule.ValidationParameters["peanuts"] = this._peanutsProperty;
rule.ValidationParameters["ordertype"] = this._orderTypeProperty;
yield return rule;
}
}
希望这有帮助