如何在ASP规则违反的情况下获得数据库验证。净MVC吗?

时间:2021-04-21 04:09:00

On the NerdDinner example a set of business rules are written to validate the data on a model. Things like empty strings are checked for and by calling modelObject.GetRuleViolations() you can get them all. But there's another layer of validation which is the database. For example, the datetime field is left for validation to the database, which only accepts a string that can be converted into a DateTime object.

在NerdDinner示例中,编写了一组业务规则来验证模型上的数据。检查空字符串之类的东西,并通过调用modelobject . getrule()来获取它们。但是还有另一层验证就是数据库。例如,datetime字段留给数据库进行验证,数据库只接受可以转换为datetime对象的字符串。

The problem I see is that modelObject.GetRuleViolations() never return the violation for datetime. So even when it is correctly prevented from saving the record and the form is shown back specifying there's an error and highlighting the datetime field, there's no specific error message. Is there a way to get the database validation errors among the business rules validation errors?

我看到的问题是,modelobject . getrule()从不返回datetime的违例。因此,即使正确地阻止它保存记录,并且表单显示回来指定有错误并突出显示datetime字段,也没有特定的错误消息。是否有一种方法可以在业务规则验证错误中获取数据库验证错误?

3 个解决方案

#1


2  

You need to catch the exceptions thrown by your Data Access Layer, and convert those into calls which update the ModelState to indicate the errors in question. There's not really a good way to do this on a global level, since specific SQL errors will only be able to be interpreted at the time they're called, rather that handled in a generic way.

您需要捕获由数据访问层抛出的异常,并将其转换为调用,该调用更新ModelState以指示存在的错误。在全局级别上实现这一点并不是很好,因为特定的SQL错误只能在调用时进行解释,而不是以通用的方式处理。

#2


0  

I don't remember the exact code from NerdDinner, though I have looked at it. However, in my applications, I typically do a raiserror('Some error',16,1) in the database and use a try/catch in my controller / model like so:

我不记得NerdDinner的确切代码,尽管我看过。但是,在我的应用程序中,我通常会在数据库中执行一个raiserror(“一些错误”,16,1),并在我的控制器/模型中使用try/catch:

public void DoSomething() 
{
    try
    {
        // Some database activity
    } 
    catch (SqlException ex) 
    {
        ViewData["ErrorMessage"] = ex.Message;
    }
}

I can then render the error in my view however I want.

然后我可以在我的视图中以我想要的方式呈现错误。

#3


0  

Your assumption is wrong. The datetime field is not left for validation on the database.

你的假设是错误的。datetime字段没有留给数据库进行验证。

One thing I had to grok in the NerdDinner/MVC lessons was that validation will occur in both the Dinner.cs OnValidate() partial method and in the DinnersController.cs in the call to UpdateModel(). This call copies the text from the screen into the Model. If, for example, it tries to copy text to a float, or parse and invalid date, it will update the ModelState and throw an error. The normal validation will not run.

我不得不在NerdDinner/MVC课程中摸索的一件事是,验证将在晚餐中出现。cs OnValidate()部分方法和DinnersController。调用UpdateModel()中的cs。这个调用将文本从屏幕复制到模型中。例如,如果它试图将文本复制到浮点数或解析和无效日期,它将更新ModelState并抛出一个错误。正常的验证将不会运行。

#1


2  

You need to catch the exceptions thrown by your Data Access Layer, and convert those into calls which update the ModelState to indicate the errors in question. There's not really a good way to do this on a global level, since specific SQL errors will only be able to be interpreted at the time they're called, rather that handled in a generic way.

您需要捕获由数据访问层抛出的异常,并将其转换为调用,该调用更新ModelState以指示存在的错误。在全局级别上实现这一点并不是很好,因为特定的SQL错误只能在调用时进行解释,而不是以通用的方式处理。

#2


0  

I don't remember the exact code from NerdDinner, though I have looked at it. However, in my applications, I typically do a raiserror('Some error',16,1) in the database and use a try/catch in my controller / model like so:

我不记得NerdDinner的确切代码,尽管我看过。但是,在我的应用程序中,我通常会在数据库中执行一个raiserror(“一些错误”,16,1),并在我的控制器/模型中使用try/catch:

public void DoSomething() 
{
    try
    {
        // Some database activity
    } 
    catch (SqlException ex) 
    {
        ViewData["ErrorMessage"] = ex.Message;
    }
}

I can then render the error in my view however I want.

然后我可以在我的视图中以我想要的方式呈现错误。

#3


0  

Your assumption is wrong. The datetime field is not left for validation on the database.

你的假设是错误的。datetime字段没有留给数据库进行验证。

One thing I had to grok in the NerdDinner/MVC lessons was that validation will occur in both the Dinner.cs OnValidate() partial method and in the DinnersController.cs in the call to UpdateModel(). This call copies the text from the screen into the Model. If, for example, it tries to copy text to a float, or parse and invalid date, it will update the ModelState and throw an error. The normal validation will not run.

我不得不在NerdDinner/MVC课程中摸索的一件事是,验证将在晚餐中出现。cs OnValidate()部分方法和DinnersController。调用UpdateModel()中的cs。这个调用将文本从屏幕复制到模型中。例如,如果它试图将文本复制到浮点数或解析和无效日期,它将更新ModelState并抛出一个错误。正常的验证将不会运行。