在.net Mvc开发时咱们通常都会选择自带的验证机制。前端
咱们定义一个modelajax
public class ContainerModel { [Display(Name = "流水号")] public long Id { get; set; } [Display(Name = "委托业务流水号")] [Required(ErrorMessage = "委托业务流水号不能为空")] public long ConsignId { get; set; } [Display(Name = "所属人")] public long? OwnerId { get; set; } }
控制器,用ModelState.IsValid进行验证,ExpendErrors是一个拓展方法,获取验证失败的字段json
public ActionResult Create(ContainerModel model) { if (ModelState.IsValid) { //... } else { log.Warn("失败,数据不完整-" + this.ExpendErrors()); return Json(...); } } }
咱们都知道,当从前端页面传入参数时,ConsignId字段是必须传的,可是 Id和OwnerId呢?后端
OwnerId是可空类型,前端固然能够不传参数,或者传一个空值的参数也能够,验证经过。测试
Id是不可空int类型,咱们能够测试一下:ui
$.ajax({ type: "Post", url: "Create", data: { Id:0, ConsignId:10, OwnerId:1, }, success: function (result) { } }, "json");
OK,验证经过this
$.ajax({ type: "Post", url: "Create", data: { ConsignId:10, OwnerId:1, }, success: function (result) { } }, "json");
OK,验证也经过url
$.ajax({ type: "Post", url: "Create", data: { Id:, ConsignId:10, OwnerId:1, }, success: function (result) { } }, "json");
这时候就不能经过了,监控ExpendErrors获得Id字段是必须的,断点到控制器Create方法,看到传过来的Model.Id=0也是有值的,这就奇怪了,明明有值的,也没设置Required验证,为何就通不过了呢?spa
原来,是我没有理解验证机制的原理,验证机制是发生在前端参数转化为后端model属性时候发生的,也就是执行目标Action方法以前,model的Id为不可空的Int类型,而参数传递了一个空值,因此验证不一样过。解决办法就是:要么不传该参数,要传就传有初始化值的参数。.net