Asp.net MVC表单验证相关

时间:2022-12-04 20:24:10

I don't know whether people have already asked this question or they haven't seen this problem or whatever.

我不知道人们是否已经问过这个问题,或者他们没有看到这个问题或其他什么。

I am Creating Strongly type view for each Create view.

我正在为每个创建视图创建强类型视图。

I am validating the form at server side by making partial class of LINQ class Entities.

我通过制作LINQ类实体的部分类来验证服务器端的表单。

By adding Function Like

通过添加功能类似

public IEnumerable<RuleViolation> GetRuleViolations()
    {
        if (String.IsNullOrEmpty(Name))
            yield return new RuleViolation("Name is Required", "Name");
        if (String.IsNullOrEmpty(Date.ToString()))
            yield return new RuleViolation("Date is Required", "Date");
        yield break;
    }

My controller action is structured like alt text http://www.scottgu.com/blogposts/mvcpreview5/step15.png

我的控制器操作的结构类似于替代文本http://www.scottgu.com/blogposts/mvcpreview5/step15.png

Problem :

If Name Field Length is Varchar2(10), And user enters name exceeded this limit then product (see image) object will have name as Empty string.

如果Name Field Length为Varchar2(10),并且用户输入的名称超出此限制,则product(参见图像)对象的名称将为Empty string。

More over Other Problems are same as above like date If user doesn't Enter Date then also object will have date something like 1/1/0001.

更多其他问题与上述日期相同如果用户没有输入日期,那么对象也会有类似1/1/0001的日期。

Summary : Should we use this method? Or to use Method like get all elements by using FormColletion or Request.Form...

摘要:我们应该使用这种方法吗?或者使用像使用FormColletion或Request.Form获取所有元素的方法...

Cam you give me the best suggestion for it?

Cam你给我最好的建议吗?

Also see Justin_etheredge's post

另见Justin_etheredge的帖子

2 个解决方案

#1


There are lots of potential solutions to this problem.

这个问题有很多潜在的解决方案。

  • For strings: I use validators that use reflection to get the maximum lengths of the strings from the column attributes on the LINQ entity properties and check these. Alternatively you could handle the error that occurs on insert if the column would be truncated.

    对于字符串:我使用验证器使用反射从LINQ实体属性的列属性中获取字符串的最大长度并检查这些。或者,如果列被截断,您可以处理插入时发生的错误。

  • For dates: You could do a sanity check on the date (i.e., must be after some reasonable date) that must be entered by the user or for dates that can be automated, use a database generated default and mark the property as autogenerated and read-only in the designer. Don't put these dates on the form so they aren't set in the entity when you post the page. This works for "Create Date", etc. For modification dates, do something similar, but have the value generated by an update trigger instead of the default on update.

    对于日期:您可以对用户必须输入的日期(即必须在某个合理日期之后)或可以自动化的日期进行完整性检查,使用数据库生成的默认值并将属性标记为自动生成并读取 - 只在设计师。不要将这些日期放在表单上,​​以便在发布页面时不在实体中设置它们。这适用于“创建日期”等。对于修改日期,执行类似操作,但具有更新触发器生成的值,而不是更新时的默认值。

  • For booleans (which default to false): validate the value provider has an attempted value for the field in addition to doing validation on the entity itself. Alternatively, you could make the column nullable and check that it's not null. Both are compromises, but the latter makes the data model fit the validation framework so I prefer the former.

    对于布尔值(默认为false):除了对实体本身进行验证之外,验证值提供程序还具有该字段的尝试值。或者,您可以使列可为空并检查它是否为空。两者都是妥协,但后者使数据模型适合验证框架,所以我更喜欢前者。

#2


That's one way of doing things -- but it pretty much breaks the MVC pattern. The way you're performing the checks there is your basically allowing LINQ and the context to handle it -- which is why youre getting issues. Ideally, you want to create a layer between your controller and the actual data -- i.e. a Service layer for example (the business intelligence (BI) layer as it's called in teh business world).

这是一种做事方式 - 但它几乎打破了MVC模式。您执行检查的方式是基本上允许LINQ和上下文来处理它 - 这就是您遇到问题的原因。理想情况下,您希望在控制器和实际数据之间创建一个层 - 例如,服务层(商业智能(BI)层,因为它在商业世界中被调用)。

In that service layer is where you would implement your rules -- such as the length of a name, the validity of a date, what is and isnt allowed. If there's something wrong there, then you can bubble up the errors and have the controller deal with them.

在该服务层中,您将实施规则 - 例如名称的长度,日期的有效性,允许的内容和不允许的内容。如果那里出现了问题,那么你可以冒出错误并让控制器处理它们。

Ideally you want to create a level of abstraction between your controllers and your actual logic.

理想情况下,您希望在控制器和实际逻辑之间创建一个抽象级别。

I'll see if i can get an example up shortly (Something's come up at the moment...)

我会看看我是否可以很快得到一个例子(此刻有些东西......)

#1


There are lots of potential solutions to this problem.

这个问题有很多潜在的解决方案。

  • For strings: I use validators that use reflection to get the maximum lengths of the strings from the column attributes on the LINQ entity properties and check these. Alternatively you could handle the error that occurs on insert if the column would be truncated.

    对于字符串:我使用验证器使用反射从LINQ实体属性的列属性中获取字符串的最大长度并检查这些。或者,如果列被截断,您可以处理插入时发生的错误。

  • For dates: You could do a sanity check on the date (i.e., must be after some reasonable date) that must be entered by the user or for dates that can be automated, use a database generated default and mark the property as autogenerated and read-only in the designer. Don't put these dates on the form so they aren't set in the entity when you post the page. This works for "Create Date", etc. For modification dates, do something similar, but have the value generated by an update trigger instead of the default on update.

    对于日期:您可以对用户必须输入的日期(即必须在某个合理日期之后)或可以自动化的日期进行完整性检查,使用数据库生成的默认值并将属性标记为自动生成并读取 - 只在设计师。不要将这些日期放在表单上,​​以便在发布页面时不在实体中设置它们。这适用于“创建日期”等。对于修改日期,执行类似操作,但具有更新触发器生成的值,而不是更新时的默认值。

  • For booleans (which default to false): validate the value provider has an attempted value for the field in addition to doing validation on the entity itself. Alternatively, you could make the column nullable and check that it's not null. Both are compromises, but the latter makes the data model fit the validation framework so I prefer the former.

    对于布尔值(默认为false):除了对实体本身进行验证之外,验证值提供程序还具有该字段的尝试值。或者,您可以使列可为空并检查它是否为空。两者都是妥协,但后者使数据模型适合验证框架,所以我更喜欢前者。

#2


That's one way of doing things -- but it pretty much breaks the MVC pattern. The way you're performing the checks there is your basically allowing LINQ and the context to handle it -- which is why youre getting issues. Ideally, you want to create a layer between your controller and the actual data -- i.e. a Service layer for example (the business intelligence (BI) layer as it's called in teh business world).

这是一种做事方式 - 但它几乎打破了MVC模式。您执行检查的方式是基本上允许LINQ和上下文来处理它 - 这就是您遇到问题的原因。理想情况下,您希望在控制器和实际数据之间创建一个层 - 例如,服务层(商业智能(BI)层,因为它在商业世界中被调用)。

In that service layer is where you would implement your rules -- such as the length of a name, the validity of a date, what is and isnt allowed. If there's something wrong there, then you can bubble up the errors and have the controller deal with them.

在该服务层中,您将实施规则 - 例如名称的长度,日期的有效性,允许的内容和不允许的内容。如果那里出现了问题,那么你可以冒出错误并让控制器处理它们。

Ideally you want to create a level of abstraction between your controllers and your actual logic.

理想情况下,您希望在控制器和实际逻辑之间创建一个抽象级别。

I'll see if i can get an example up shortly (Something's come up at the moment...)

我会看看我是否可以很快得到一个例子(此刻有些东西......)