MVC 2 唯一索引验证

发布于 2024-10-07 13:41:06 字数 582 浏览 3 评论 0原文

有没有办法验证模型上应该唯一的属性?例如,用户可以创建和编辑“产品”,但他们不应该能够使用现有的 ProductCode 创建产品,也不应该能够编辑产品并将 ProductCode 更改为已存在的代码。

我尝试过使用自定义属性。

public class Unique : ValidationAttribute
{
    public override bool IsValid(object value)
    {
        var products = Repository.Store.Products.Where(x => x.ProductCode == value);
        return (products.Count() == 0);
    }
}

我可以用此解决方案涵盖的所有内容是,当代码已存在于数据库中时,不允许用户插入/更新产品。这不允许用户编辑现有产品,因为当他们提交时,它将看到代码已经存在(它将是他们尝试编辑的产品的代码。)并返回 false。

MVC 2 中是否没有办法处理唯一索引,我已经搜索了几个小时,甚至在 stackoverflow 上找到了其他主题,但没有任何可靠的解决方案。

Is there a way to validate a property that should be unique on a model? For example, a user can create and edit a "Product", but they shouldn't be able to create a Product with an existing ProductCode, nor should they be able to edit a Product and change the ProductCode to a code that already exists.

I have tried using a custom attribute.

public class Unique : ValidationAttribute
{
    public override bool IsValid(object value)
    {
        var products = Repository.Store.Products.Where(x => x.ProductCode == value);
        return (products.Count() == 0);
    }
}

All that I can cover with this solution is not allowing the user to insert/update a Product when the code already exists in the DB. This does not allow the user to edit an existing product because when they submit, it will see that the code already exists(it will be the code for the product they are trying to edit.) and returns false.

Is there no way of dealing with a unique index in MVC 2, I have searched for hours, even found other topics on stackoverflow, but nothing with a solid solution.

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

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

发布评论

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

评论(3

不回头走下去 2024-10-14 13:41:06

只需让插入或更新失败,然后向用户返回适当的错误消息即可。无论如何,预先检查都是有问题的,因为另一个用户总是有可能在您检查后立即修改数据库。

下面是如何插入对象并确定它是否由于唯一约束而失败的示例:

INSERT INTO MyTable (column1, column2, column3) 
    SELECT @param1, @param2, @param3
    WHERE NOT EXISTS (SELECT * FROM table WHERE id = @param4)

如果该对象已存在,则这将修改 0 行。如果没有,那么它将修改 1 行。如果出现任何其他问题,您将获得例外。这也是相当高效的(至少在 SQL Server 中)。正如您所希望的那样,它会导致索引查找,然后更新索引。

Just let the insert or update fail and then return an appropriate error message to the user. Checking up front is problematic anyway since there's always a chance that another user will modify the database immediately after your check.

Here's an example of how you can insert an object and determine whether or not it failed due to a unique constraint:

INSERT INTO MyTable (column1, column2, column3) 
    SELECT @param1, @param2, @param3
    WHERE NOT EXISTS (SELECT * FROM table WHERE id = @param4)

If the object already exists, this will modify 0 rows. If it does not, then it will modify 1 row. If anything else goes wrong, you'll get an exception. This is also quite efficient (at least in SQL server). It results in an index seek followed by an index update, just as you would hope.

手长情犹 2024-10-14 13:41:06

我在相关领域的 MVC 方面遇到了一些困难。

我的问题的部分答案是,您“可能”应该有一个单独的模型来插入和更新对象。

这样您就可以在插入模型上拥有自定义属性。

否则,只需将其视为插入方法中的正常代码检查,而不是在自定义属性中。

I struggled with MVC a little in a related area.

Part of the answer gleaned to my question was that you should "probably" have a seperate model for Insert and Update of an object.

That way you could have your custom attribute just on the insert model.

Otherwise, just take care of this as a normal code check in your insert method, not in a custom attribute.

方圜几里 2024-10-14 13:41:06

好的,我明白了......

您可以对对象上某些唯一 ID 的存在进行“不等于”检查吗 - 这样您可以检查产品代码是否存在,但不检查当前产品。

Ok I see....

Can you do a "does not equal" check on the exists for some unique ID on the object - that way you check for the existence of the product code BUT not on the current product.

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