asp.net-mvc – 更新我的模型然后重新评估IsValid?
发布时间:2020-05-28 02:29:26 所属栏目:asp.Net 来源:互联网
导读:我将一些值传递给我的控制器动作,一切都很好。 POST设计中将缺少两个属性。 然后我设置了缺失的值,但后来我想验证模型,它仍然是假的,因为看起来ModelState没有赶上我的更改。 [HttpPost, Authorize]public ActionResult Thread(int id, string groupSlug,
|
我将一些值传递给我的控制器动作,一切都很好。 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 – 如何设置通配符(*)子域?
- asp.net – HTTP错误403 – 禁止
- asp.net-mvc – ASP.NET MVC – 接口类型上的自定义模型绑定
- asp.net-core – asp.net核心身份中的多个SubDomain的cooki
- asp.net – Windows Azure我可以在同一个额外的小实例或小实
- asp.net – 兼容模式下的IE9无法正确显示CSS样式
- asp.net – @RenderSection在嵌套剃刀模板
- asp.net – AspNetSession布局渲染器不工作
- asp.net-mvc – “安全感知”动作链接?
- asp.net-core – RemoteIpAddress始终为null
推荐文章
站长推荐
- 如何在ASP.NET中混合WebForms和MVC
- asp.net – SignalR 3的开发状况如何?
- asp.net-mvc – 如何在ASP.NET MVC中禁用客户端和
- 将用户添加到角色ASP.NET身份
- asp.net-mvc-3 – 在ASP.NET MVC 3中覆盖/禁用授
- asp.net-mvc-4 – 在Startup.Auth.cs之外配置的C
- ASP.NET私有成员字段在回发时丢失了值
- asp.net – ASP .Net Web API下载图像为二进制
- asp.net-mvc – 为什么调用base.OnActionExecuti
- asp.net-mvc – ASP.NET MVC – 本地化路由
热点阅读
