asp.net-mvc – 更新我的模型然后重新评估IsValid?
发布时间:2020-12-16 00:20:44 所属栏目:asp.Net 来源:网络整理
导读:我将一些值传递给我的控制器动作,一切都很好。 POST设计中将缺少两个属性。 然后我设置了缺失的值,但后来我想验证模型,它仍然是假的,因为看起来ModelState没有赶上我的更改。 [HttpPost,Authorize]public ActionResult Thread(int id,string groupSlug,C
我将一些值传递给我的控制器动作,一切都很好。 POST设计中将缺少两个属性。
然后我设置了缺失的值,但后来我想验证模型,它仍然是假的,因为看起来ModelState没有赶上我的更改。 [HttpPost,Authorize] public ActionResult Thread(int id,string groupSlug,Comment comment,string submitButton) { comment.UserID = UserService.UID; comment.IP = Request.UserHostAddress; UpdateModel(comment); //throws invalidoperationexception if (ModelState.IsValid) // returns false if i skip last line { //save and stuff //redirect } //return view } 什么是最简洁的方法来轻拍模型状态并告诉它一切都会好的同时仍然验证从用户的POST绑定的所有其他内容 解决方法
如果模型需要缺少的值,但在绑定之后才会提供,则可能需要清除ModelState中这两个值导致的错误。
[HttpPost,string submitButton) { comment.UserID = UserService.UID; comment.IP = Request.UserHostAddress; //add these two lines ModelState["comment.UserID"].Errors.Clear(); ModelState["comment.IP"].Errors.Clear(); UpdateModel(comment); //throws invalidoperationexception if (ModelState.IsValid) // returns false if i skip last line { //save and stuff //redirect } //return view } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- asp.net-mvc – 在MVC 3模型ID属性中将ScaffoldColumn属性设
- asp.net viewstate加密问题
- VS 2015 Update 2 – 调试时不存在变量,为什么?
- asp.net – ASP Core,Identity Server 4和Angular 2中的外部
- asp.net-mvc – 来自EDMX文件的MVC模型 – 为其添加自定义注
- asp.net – 运行时从Microsoft.AspNet.WebApi.HelpPage版本
- asp.net – 如何将现有Azure Web角色转换为新式Azure网站?
- 认证 – asp.net mvc 3:Page.User.IsInRole(“xy”)返回nu
- asp.net – 使用Global.asax有什么优点和缺点?
- ASP.NET MVC Web应用程序中的控制器是否应该调用存储库,服务
推荐文章
站长推荐
- asp.net-core – ASP.NET vNext项目参考
- asp.net-mvc-4 – MapRoute for overload Action
- asp.net – 在维护期间重定向站点访问者
- asp.net-mvc – Elmah.MVC在IIS Express下工作但
- asp.net-mvc – 使用相同的密钥从Request.Files获
- 在ASP.NET MVC 3中使用Ajax和JsonResult
- asp.net – 在同一行(左右)对齐标签和文本框
- asp.net – 如何在单击“提交”按钮后显示requir
- Aspect-Oriented Programming : Aspect-Oriented
- asp.net – 嵌套的用户控件的控件为null
热点阅读