簡體   English   中英

必需如果不適用於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");
    }
}

當我發布: {"AorB" : "B", "Bar" : 1}我收到一條消息,因為“由於AorB == B而需要Foo”,並且系統返回BadRequest

當我發布: {"AorB" : "B", "Foo" : "foo"}我沒有收到消息,系統返回OK。

好的,未驗證的原因是因為它應該是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; }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM