0

使用新的 WebAPI 2.0 RC1 预发布位......给定这个方法:

[HttpPut("{sampleForm}/{id?}")]
public HttpResponseMessage PutSampleForm(SampleForm sampleForm, int? id)
{
    if (!ModelState.IsValid)
    {
        // handle invalid model
    }

    // Insert valid model into DB with EF

    return Request.CreateResponse(HttpStatusCode.OK);
}

它标有可为空的 id,但如果 id 实际上为空,则 ModelState 也被标记为无效......这是预期的,还是我可以做些什么让 ModelState 知道它需要忽略可为空的参数?

4

1 回答 1

0

是的,您也会在模型的属性上使用问号,如下所示:

private int? id {get; set;}
于 2013-09-24T16:33:40.883 回答