0

我正在尝试验证一个模型,其中规则并不总是相同并且取决于模型中的其他属性。这样做的最佳方法是什么?下面的例子:

假设示例 1

将 MVVM 模式与 MVC 3 结合使用。我的(假设的)ViewModel 如下所示:

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,否则显示验证消息“样本总量不够高”

等等……等等……

4

2 回答 2

0

我将扩展 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;
    }
}

然后将验证标签放在适当的模型属性上:

[PeanutOrder("Peanuts", "OrderType")]
public int Peanuts{ get; set; }

public string OrderType { get; set; }

希望这可以帮助!

于 2013-10-03T05:10:40.540 回答
0

ValidationAttribute您可以使用自己的自定义验证属性进行扩展。

public class CustomAttribute : ValidationAttribute
{    
    protected override ValidationResult IsValid(object value, ValidationContext validationContext)
    {
        // validation logic
    }
}
于 2013-10-02T20:37:15.157 回答