Asp.net Mvc2 数据注释验证(客户端有效,服务器端无效??)
我正在使用带有元数据注释的实体模型。我的控制器方法如下所示...
if (!ModelState.IsValid)
{
return View(model);
}
else
{
UpdateModel(model);
repo.Save();
return RedirectToAction("Index");
}
如果我在视图中启用客户端验证,我将收到元数据类中每个属性的错误。如果我取消客户端验证,保存到数据库时会抛出错误,而不是返回带有错误摘要的视图。
这是我观点的顶部......
<% using (Html.BeginForm())
{%>
<%: Html.ValidationSummary(true) %>
我尝试在调试和发布模式下运行而不进行调试(ctrl + f5)以及设置断点和调试(f5),但在没有进行客户端验证的情况下获得客户端验证似乎很奇怪服务器端验证。我在这里缺少什么?
I'm using an entity model with metadata annotations. My controller method looks like this...
if (!ModelState.IsValid)
{
return View(model);
}
else
{
UpdateModel(model);
repo.Save();
return RedirectToAction("Index");
}
If I enable client side validation in the view I'll get the error per the Attributes from the metadata class. If I take clientside validation out, error gets thrown from saving to the DB rather than return the view with an Error Summary.
This is the top portion of my view....
<% using (Html.BeginForm())
{%>
<%: Html.ValidationSummary(true) %>
I've tried running without debugging (ctrl + f5) in debug and release mode as well as setting breakpoints and Debugging (f5) but it just seems weird to get Client side validation without server side validation. What am I missing here?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
UpdateModel 从表单集合、路由参数等填充模型,并在服务器端进行验证。您需要在更新后检查 ModelState.IsValid。通常的模式是...
请注意,TryUpdateModel 捕获异常并在引发异常时返回 false。如果不是,则简单地返回 ModelState.IsValid。
UpdateModel populates the model from form collection, routing parameters, etc. and does validation on the server side. You need to check ModelState.IsValid after updating. The usual pattern is...
Note that TryUpdateModel catches exceptions and returns false if they're raised. If they aren't, then it simple returns ModelState.IsValid.