MVC2实体框架4中REQUIRED字符串属性的服务器端验证不起作用 [英] Server-side validation of a REQUIRED String Property in MVC2 Entity Framework 4 does not work

查看:106
本文介绍了MVC2实体框架4中REQUIRED字符串属性的服务器端验证不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图让实体框架字符串属性的服务器端验证工作。其他服务器端验证(如数据类型验证和所需的dateTime和数字EF属性)正在工作。

I'm trying to get server-side validation of an Entity Framework String Property to work. Other server-side validation such as data type validation and required dateTime and numeric EF properties are working.

这在VS 2010,.Net 4.0,MVC2 + Cloud,ADO.Net实体框架。

This in VS 2010, .Net 4.0, MVC2 + Cloud, ADO.Net Entity Framework.

字符串属性我遇到问题被映射到SQL 2008,Varchar(50)不可空列。

The String Property I am having issues with is mapped to a SQL 2008, Varchar(50) non-nullable column.

当我尝试发布到我的创建操作一个空字符串为此属性,我得到以下错误。

When I try to post to my Create action with an empty string for this Property, I get the follwing error.


异常详细信息:System.Data.ConstraintException:此属性不能设置为空值。

Exception Details: System.Data.ConstraintException: This property cannot be set to a null value.

当我发布到一个空白的动作时,我成功地获得了必填的字段验证消息。

When I post to the action with a blank space, I successfully get a required field validation message.

我已经尝试使用数据注释和ClientSideValidation,但是在部分视图和jquery对话框中使用ClientSideValidation似乎有问题。

I have tried using Data Annotations and ClientSideValidation but there seems to be issues with ClientSideValidation working on partial views and jquery dialogs.

这是实体框架中的原始自动生成代码。

Here is the orginal autogenerated code from the entity framework.

[EdmScalarPropertyAttribute(EntityKeyProperty=false, IsNullable=false)]
[DataMemberAttribute()]
public global::System.String GradeTypeName
{
    get
    {
        return GradeTypeName;
    }
    set
    {
        OnGradeTypeNameChanging(value);
        ReportPropertyChanging("GradeTypeName");
        _GradeTypeName = StructuralObject.SetValidValue(value, false);
        ReportPropertyChanged("GradeTypeName");
        OnGradeTypeNameChanged();
    }
}

根据Action方法的签名(CREATE或EDIT),在调用UpdateModel()方法之前或方法中,异常可能会发生。内部异常是在model.designer.cs文件的下方。

Depending on the signature of the Action method (CREATE or EDIT), the exception can occur before stepping into the method or within the method when UpdateModel() is called. The inner exception is at the line below from the model.designer.cs file.

_GradeTypeName = StructuralObject.SetValidValue(value, false);

我已经能够在一个简单的mvc2网络应用程序上重现这个。

I have been able to reproduce this on a simple mvc2 web application.

推荐答案

我有同样的问题一段时间。我在这里找到了一个解释: http://mvcmusicstore.codeplex.com/workitem/6604。简而言之,异常 System.Data.ConstraintException:此属性不能设置为空值由Entity的属性验证抛出。当您的mvc应用程序尝试将表单字段绑定到相应的实体属性(它称为 PreBinding Validation ),并且在提交表单时会发生此验证。由于该字段为空(因此转换为null),绑定器尝试将一个空值绑定到属性,这违反了您的实体属性的非空限制。

i was having the same problem for a while. I have found a piece of explanation here: http://mvcmusicstore.codeplex.com/workitem/6604 . To put it in a nutshell, the exception "System.Data.ConstraintException: This property cannot be set to a null value" is thrown by Entity's Property Validation. This validation is performed when your mvc application tries to bind the form field to the corresponding entity property( it's called PreBinding Validation, and it occurs when submitting the form). As the field is empty( therefore convert to null), the binder tries to bind a null value to the property, which violates the Non-Null constraint on your entity's property.

但是如果你发布一个空白字段(不同于空,因此为null)实体验证通过(因为该属性不再设置为空值),然后您可以从必需注释验证看到消息,这是在预绑定之后执行的(它是 PostBinding验证)。

But if you post with a blank field ( that is different from empty, therefore null) Entity validation passes( as the property is not set to a null value anymore), and then your see the message from the "Required" annotation validation, that is performed after the prebinding (it's PostBinding Validation).

解决方法是使用注释 [DisplayFormat(ConvertEmptyStringToNull = false )] ,告诉binder不要将空字符串转换为空。

A workaround is to use the annotation [DisplayFormat(ConvertEmptyStringToNull = false)] that tells to the binder not to convert an empty string to null.

  [Required]
  [DisplayFormat(ConvertEmptyStringToNull = false)]
  public string YourStringProperty { get; set;}

希望这有帮助!

这篇关于MVC2实体框架4中REQUIRED字符串属性的服务器端验证不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆