RequiredIf 不适用于 int 属性

RequiredIf not working for int property

我正在使用 ExpressiveAnnotations 对我的属性进行一些验证。现在,我面临无法验证类型为 int:

的 属性 的问题
public class MyModel {

    public string AorB { get; set; }

    [RequiredIf("AorB == 'B'")]
    public string Foo { get; set; }

    [RequiredIf("AorB == 'B'")]
    public int Bar { get; set; }
}

我的控制器

public class MyController : ApiController
{

    public IHttpActionResult Post(MyModel myModel)
    {
        if(ModelState.IsValid)
        {
            // do something
            return Ok();
        }

        return BadRequest("To bad");
    }
}

当我 POST: {"AorB" : "B", "Bar" : 1} 我收到一条消息 "Foo is required because of AorB == B" 和系统 returns BadRequest

当我 POST: {"AorB" : "B", "Foo" : "foo"} 我没有收到消息,系统 returns 好的。

好的,它没有验证的原因是因为它应该是 int?所以模型看起来像这样:

public class MyModel {

    public string AorB { get; set; }

    [RequiredIf("AorB == 'B'")]
    public string Foo { get; set; }

    [RequiredIf("AorB == 'B'")]
    public int? Bar { get; set; }
}