使用 JsonResult 时,使用哪些技术将 MVC ModelState 验证错误传递回客户端?

发布于 2024-08-06 17:56:50 字数 198 浏览 6 评论 0原文

我在这里大声思考,所以让我知道我是否需要澄清......

在 ajax 密集型网站上,当使用 JsonResult 将信息传递回客户端时,使用了哪些技术、模式、最佳实践来传递信息ModelState 验证错误返回给客户端?

我在视图模型上使用 xVal 和 castle 验证,是否有某种标准可以让 jquery 验证显示来自 ajax 响应的错误?

I'm sort of thinking out loud here, so let me know if I need to clarify...

on ajax heavy sites, when using JsonResult to pass information back to the client, what techniques, patterns, best practices are being used to pass ModelState validation errors back to the client?

I am using xVal and castle validation on my view models, is there some sort of standard to get jquery validate to display errors coming from ajax responses?

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

心欲静而疯不止 2024-08-13 17:56:50

我不知道任何最佳实践,但我可以告诉你我在最近的项目中做了什么。基本上,我为所有 JSON 保存结果定义了一个名为 IJSONValidationResult 的接口。该接口由两个属性组成,一个布尔 IsValid 指示保存/操作是否有效,以及一个错误列表。该类随后填充了 ModelState 验证错误。

我在客户端使用 JavaScript 来解析 JSON 结果并对页面进行适当的更新。例如,如果它有效,则从编辑页面重定向到显示保存结果的列表页面,或者如果存在错误,则在之前隐藏的 div 中向用户显示它们。

没什么太令人兴奋的,但它的开销很低,而且非常简单。

I know of no best practices, but I can tell you what I did on a recent project. Basically, I defined an interface for all JSON save results called IJSONValidationResult. This interface consisted of two properties, a bool IsValid indicating if the Save/Action was valid and a List of the errors. The class was than populated with the ModelState validation errors.

Than I used javascript on the client side to parse the JSON result and make the appropriate updates to the page. As an example, if it was valid than redirect from the edit page to the list page showing the saved result, or if there were errors, show them to the user in a previously hidden div.

Nothing too exciting, but it was low overhead and pretty straight forward.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文