MVC:客户端验证失败,但数据仍然发布

发布于 2024-10-04 11:29:02 字数 568 浏览 1 评论 0原文

如上所述,我实现了客户端验证没有问题,并且它根据需要弹出相关消息。但是,即使表单未处于有效状态,提交按钮仍然显示为提交表单。

控制器方法立即将其抛出,因为 ModelState 无效,但是是否有一个标志/属性我可以检查客户端以防止帖子发生?

更新: 问题似乎是因为表单是使用 jquery post 异步提交的,因此我的 javascript 方法无论验证状态如何都会发布数据。所以我正在寻找的是,在发表这篇文章之前,按照 if( [Property/Method which indicates MVC Model State] == false ) return false; 的方式做一些事情,但是,我正在努力寻找这样的方法或属性。

我考虑过实现一个检查函数,该函数可以识别“输入验证错误”类是否应用于我的任何表单字段。使用 JQuery,它非常简单,但它不太适合我。有人对此有什么意见吗?

一方面,这看起来有点像黑客,因为在验证表单后我会有效地迭代表单以查看它是否确实有效。另一方面,如果同一页面上有多个表单,我不确定 MVC JS 如何识别模型状态,而如果我自己做,我可以识别“对于此表单的帖子,我”我对这些领域感兴趣'。

As above, I'm implemented the client side validation no problem, and it pops up the relevant messages as required. However, the submit button still appears to submit the form even though the form is not in a valid state.

The controller method throws it out immediately, because ModelState is not valid, but is there a flag/property I can check client side to prevent the post from happening at all?

UPDATE:
The problem appears to be because the form is submitted asynchronously using a jquery post, hence my javascript method is posting the data regardless of the validation state. So what I'm looking for is, before I do that post, to do something along the lines of if( [Property/Method which indicates MVC Model State] == false ) return false;, however, I'm struggling to find such a method or property.

I've considered implementing a check function which identifies whether the 'input-validation-error' class is applied to any of my form fields. With JQuery, it's pretty simple, but it doesn't sit to well with me. Does anyone have any opinions on doing this?

On the one hand, it seems a bit of a hack because I'm effectively iterating through the form after it's been validated to see if it's actually valid. On the other hand, I'm not sure how the MVC JS would identify the modelstate if there were multiple forms on the same page, whereas if I was to do it myself, I could identify 'for the post of this form, I'm interested in these fields'.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文