我希望过滤所有模型验证错误。例如:
我的HealthCenterViewModel
public class HealthCenterViewModel {
[Required]
public string Name { get; set; }
public string FaxNumber { get; set; }
[Required]
public AddressModel Address { get; set; }
...
}
public class AddressModel {
[Required]
public string Street { get; set; }
[Required]
public string StreetNumber { get; set; }
... }
我想通过包含的子模型错误获取所有模型验证错误,因此我使用此ValidationActionFilter
public class ValidationActionFilter : ActionFilterAttribute
{
public override void OnActionExecuting(HttpActionContext actionContext)
{
var modelState = actionContext.ModelState;
if (!modelState.IsValid)
actionContext.Response = actionContext.Request
.CreateErrorResponse(HttpStatusCode.BadRequest, modelState);
}
}
问题是我总是回到这个:
model.Address:"The Address field is required."
model.Email:"The Email field is required."
model.PhoneNumber:"The PhoneNumber field is required."
model.ShortName:"The ShortName field is required."
但不需要Address
模型中的哪个属性。
是否有可能以某种方式从地址中获取验证错误消息?
我希望得到类似的内容:
model.Address.Street:"The Address.Street field is required."
model.Address.StreetNumber:"The AddressStreetNumber field is required."