MVC 3 远程验证,编辑视图中重复检查的问题

发布于 2024-11-29 23:34:06 字数 379 浏览 4 评论 0原文

我正在使用远程验证来确保用户类中的电子邮件和用户名字段阻止用户输入数据库中已存在的用户名和/或电子邮件。

这在创建视图上工作得很好,但是我在编辑视图中遇到的明显问题是,当我尝试为给定用户保存一些更改时 - 我在用户名和电子邮件上收到相同的验证消息,表明它们已经存在于数据库!因此阻止我编辑任何人,因为他们的电子邮件和用户名已经存在。

我环顾四周,很惊讶我找不到与我类似的问题。我在创建页面上看到过许多重复名称/电子邮件/值验证的示例,但没有看到编辑视图中出现的不可避免的问题。

任何有关解决此问题的提示/技巧将不胜感激。也许有一种方法可以使验证仅在创建视图中起作用?虽然理想情况下,我希望在编辑视图中进行验证,只是在验证检查中排除用户自己的姓名和电子邮件。

预先感谢您的任何答复!

I am using remote validation to make sure that the email and username fields in a user class stop a user from entering a username and/or email that already exists within the database.

This works fine on the create view, however the obvious problem I run into in the edit view is that when I try save some changes for a given user - I get the same validation messages on the username and email saying that they already exist in the database! Therefore stopping me from editing anyone because their emails and usernames already exist.

I have been looking around and was surprised that I could not find a similar problem to mine. I have seen many examples of dupliate name/email/value validation on create pages but nothing on the inevitable problem that will arise in the Edit view.

Any hints/tips on a way around this would be greatly appreciated. Maybe there is a way to make the validation only work in the create view? Though ideally, I want the validation in the edit view, just excluding the user's own name and email in the validation checks.

Thanks in advance for any answers!

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

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

发布评论

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

评论(2

阳光①夏 2024-12-06 23:34:06

您应该使用视图模型。这些类是专门为满足视图的要求而设计的。控制器操作应该只将视图模型传递给视图,而不是域模型。因此,您将有两个控制器操作,一个用于插入,一个用于编辑,以及两个相应的视图模型及其各自的验证规则。

You should use view models. Those are classes which are specifically designed to meet the requirements of a view. Controller actions should take/pass only view models to views and never your domain models. So you will have two controller actions, one for inserting and one for editing, and two corresponding view models with their respective validation rules.

上课铃就是安魂曲 2024-12-06 23:34:06

我解决这个问题的方法是使用两种不同的验证方法;一种采用单个参数(用户名),另一种采用 2 个参数(新用户名、原始用户名)。 Edit 方法根据 2 个参数方法进行验证,在该方法中,它会查找新用户名,除非它与原始用户名匹配。

The way I've gotten around the problem is having 2 different validation methods; one takes a single argument (the user name) and one takes 2 arguments (the new user name, the original user name). The Edit method validates against the 2 argument method, where it looks for the new user name unless it matches the original user name.

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