[英]ASP.NET MVC Validation and Model Properties, what happens first?
我有一個帶有屬性和驗證器(注釋)的模型,它們中的大多數驗證了該屬性。
驗證器是否使用模型屬性,還是使用POST發送的原始值? 換句話說,是先設置屬性,然后進行驗證,還是先進行驗證,然后設置屬性(如果驗證沒有返回錯誤)?
您可以像下面這樣檢查控制器中的模型是否有效:
public class ProductsController : ApiController
{
public HttpResponseMessage Post(Product product)
{
if (ModelState.IsValid)
{
// Do something with the product (not shown).
return new HttpResponseMessage(HttpStatusCode.OK);
}
else
{
return Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState);
}
}
}
更多信息: ASP.NET中的模型驗證
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.