【发布时间】:2015-10-13 12:17:26
【问题描述】:
我正在使用 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");
}
}
当我发布时:{"AorB" : "B", "Bar" : 1} 我收到一条消息“由于 AorB == B 需要 Foo”并且系统返回 BadRequest
当我 POST 时:{"AorB" : "B", "Foo" : "foo"} 我没有收到消息,系统返回 Ok。
【问题讨论】:
标签: c# asp.net-web-api2 expressiveannotations