使用好友类时MVC3验证 [英] mvc3 validation when using buddy classes

查看:93
本文介绍了使用好友类时MVC3验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我工作的一个MVC3应用程序,并有一些问题得到验证工作,我想要的。

的应用程序正在使用的好友类模型。 (这是我没有在过去使用过,我有点困惑,为什么它们被用来......反正)

我要添加必要的字段来确保提交的数据是正确的。我曾尝试将必填字段的哥们类。

当我提交表单没有客户端验证发生和调试器步入实体框架生成的code。这里是抱怨包含空值的字段是造成无效。如果我经历了所有这些步骤它终于到达控制器在我的IF(ModelState.IsValid)正显示出假的。

我有客户端验证开机。

难道我的意思好友一流水平,或在视图模型中应用数据验证?

另外一个问题是,为什么使用哥们类?对我来说,他们似乎在复杂的事情。

更新添加好友类的例子

  [MetadataType(typeof运算(CustomerMetaData))]
公共部分类客户
{
    公共字符串先决
    {
        得到
        {
            VAR DESC =(优先级)优先;
            返回desc.ToString()更换('_','')。
        }
    }内部类CustomerMetaData
{
    [需要]
    [DisplayName的(先决)]
    公共字符串先决{搞定;组; }


解决方案

巴迪班班的元数据将数据标注属性时,你是不是在原来的类,即不能编辑它的控制权。典型的情况是,当被像实体框架的ORM生成的类。

  //不能修改这个类
公共部分类YourClass {
  公共字符串SomeField {搞定;组;}
}//添加部分类
[MetadataType(typeof运算(YourClassMetadata))]
公共部分类YourClass {
}//一个元数据类
公共类YourClassMetadata
{
  [必需(的ErrorMessage =是必需的一些字段)
  公共字符串SomeField {搞定;组;}
}

你确定你有 [MetadataType(typeof运算(YourClassMetadata))]

更多关于哥们类<一href=\"http://www.paraesthesia.com/archive/2010/01/28/separating-metadata-classes-from-model-classes-in-dataannotations-using-custom.aspx\"相对=nofollow>这里和<一个href=\"http://mtaulty.com/CommunityServer/blogs/mike_taultys_blog/archive/2009/05/01/metadata-classes-a-force-for-good-or-evil.aspx\"相对=nofollow>这里

I am working on an mvc3 application and having some problems with getting validation to work as I want.

The application is using buddy classes for the models. (This is something I haven't used in the past and I am a little confused why they are used...anyway)

I want to add required fields to ensure data been submitted is correct. I have tried adding the required field to the buddy class.

When I submit the form no client-side validation takes place and the debugger steps into the entity frameworks generated code. Here is complains that the fields that contain null values are causing are invalid. If I step through all of those it finally gets to the controller where my if (ModelState.IsValid) is showing false.

I have client-side validation switched on.

Am I meant to be applying the data validation at the buddy class level or at the view model?

One other question is why use buddy classes? to me they seem to over complicate things.

Updated added an example of the buddy class

[MetadataType(typeof (CustomerMetaData))]
public partial class Customer
{
    public string Priorty
    {
        get
        {
            var desc = (Priority) Priority;
            return desc.ToString().Replace('_', ' ');
        }
    }

internal class CustomerMetaData
{
    [Required]
    [DisplayName("Priorty")]
    public string Priorty { get; set; }

解决方案

Buddy classes are metadata classes to put data annotation attributes when you are not in control of the original class i.e. can't edit it. Typical situation is when the class is generated by an ORM like Entity Framework.

//Can't edit this class
public partial class YourClass{
  public string SomeField {get; set;}
}

//Add a partial class
[MetadataType(typeof(YourClassMetadata))]
public partial class YourClass{
}

//And a metadata class    
public class YourClassMetadata
{  
  [Required(ErrorMessage = "Some Field is required")]
  public string SomeField {get; set;}
}

are you sure that you have [MetadataType(typeof(YourClassMetadata))]?

More about buddy classes here and here

这篇关于使用好友类时MVC3验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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