ModelState中始终处于必填字段认为是有效的,无论空值 [英] ModelState is always considered valid, regardless of null values in required fields
问题描述
<一个href=\"http://stackoverflow.com/questions/24240834/model-validation-why-modelstate-isvalid-always-returns-true-even-when-no-value\">I've一直 <一个href=\"http://www.asp.net/web-api/overview/formats-and-model-binding/model-validation-in-aspnet-web-api\">looking <一href=\"http://stackoverflow.com/questions/11686690/handle-modelstate-validation-in-asp-net-web-api\">around我认为我的解决方案仅仅是罚款,但不知何故 ModelState.IsValid
属性始终是真正
。
I've been looking around and I think my solution is just fine but somehow the ModelState.IsValid
property is always true
.
考虑以下code片段:
Consider the following code snippets:
[Route("address")]
[HttpPut]
[ResponseType(typeof(UserViewModel))]
public IHttpActionResult UpdateAddress([FromBody] UpdateAdressValidationModel model)
{
if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}
// irrelevant code omitted
}
[TestMethod]
public void UpdateAddress_WithoutStreet_ReturnsHttpCode400()
{
var userController = new UserController(new UserRepository(_context));
var addressInfo = new UpdateAdressValidationModel
{
City = "Ghent",
};
var response = userController.UpdateAddress(addressInfo) as BadRequestResult;
Assert.IsNotNull(response);
}
public class UpdateAdressValidationModel
{
[Required]
public string Street { get; set; }
[Required]
public int? Number { get; set; }
[Required]
public string Bus { get; set; }
[Required]
public int? PostalCode { get; set; }
[Required]
public string City { get; set; }
}
仍然给我一个有效的ModelState,即使它清楚地表明,需要的属性是空
。
我是什么俯瞰?
请注意,<一个href=\"http://stackoverflow.com/questions/8165026/testing-modelstate-is-always-valid-in-asp-net-mvc?rq=1\">manually加入
Validator.ValidateObject(model, new ValidationContext(model));
在 UpdateAddress
方法的顶部抛出一个 ValidationException
在街
字段,以便它可以在事实上验证模型。问题是:为什么自动,不是吗?
at the top of the UpdateAddress
method throws a ValidationException
on the Street
field so it can in fact validate the model. Question remains: why doesn't it automatically?
此外,<一href=\"http://stackoverflow.com/questions/17923622/modelstate-isvalid-even-when-it-should-not-be\">this不适用,因为我的模式
不是空
。
Furthermore, this isn't applicable because my model
isn't null
.
推荐答案
原来,<一个href=\"http://stackoverflow.com/questions/8165026/testing-modelstate-is-always-valid-in-asp-net-mvc/8165243#8165243\">this答案是对的,但解决方案不太符合。
Turns out that this answer had the right idea but the solution didn't quite fit.
确认情况。然后视图模型被传递到控制器。您跳过第1部分和直传球视图模型到控制器中。
Validation happens when the posted data is bound to the view model. The view model is then passed into the controller. You are skipping part 1 and passing a view model straight into a controller.
这是正确的,但所提出的解决方案,抛出一个ValidationException而不是简单地设置的IsValid
属性假
。
Which is correct, but the proposed solution throws a ValidationException instead of simply setting the IsValid
property to false
.
幸运的是,可以做这件事的具体方法:的 ApiController.Validate()
。通过添加这些行到我的单元测试它集的ModelState
无效和的不抛出异常的
Luckily, there is a specific method that can do this: ApiController.Validate()
. By adding these lines to my Unit Test it sets the ModelState
to invalid and does not throw an exception.
userController.Configuration = new HttpConfiguration();
userController.Validate(addressInfo);
这篇关于ModelState中始终处于必填字段认为是有效的,无论空值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!