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

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

问题描述

我试图让一个实体框架String属性的服务器端验证工作。其他的服务器端验证,如数据类型的验证和所需的日期时间和数字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 +云,ADO.Net Entity Framework的。

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

String属性我有映射到SQL 2008,VARCHAR问题(50)非空列。

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

当我尝试发布到我的创建行动,此属性的空字符串,我得到follwing错误。

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尝试,但似乎有问题与ClientSideValidation偏意见和jQuery对话框中工作。

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

下面是从实体框架的原单自动生成code。

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();
    }
}

根据不同的操作方法(创建或编辑)的签名,可以步入方法或当的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 Web应用程序。

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

推荐答案

我有一段时间了同样的问题。我发现了一块在这里解释:<一href=\"http://mvcmusicstore.$c$cplex.com/workitem/6604\">http://mvcmusicstore.$c$cplex.com/workitem/6604 。把它概括地说,除了 System.Data.ConstraintException:此属性不能设置为空值是由实体的属性验证抛出。此验证是当你的MVC应用程序试图表单域绑定到相应的实体财产进行(这就是所谓的 preBinding验证,然后将其提交表单时发生)。由于该字段为空(因此转换为空),粘结剂试图为空值绑定到属性,它违反了你的实体所拥有的非空约束。

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.

但是,如果你发布一个空白的领域(即从空不同,因此空)实体验证通过(因为属性未设置​​为空值了),然后你看到的必需的注解消息验证,即prebinding后进行(它的 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)] ,告诉到活页夹不转换为空字符串为null。

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;}

希望,这有助于!

Hope, this helps!

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

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