mvc3 validation when using buddy classes

2019-07-25 11:18发布

问题:

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

回答1:

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



回答2:

You would typically use a buddy class when it isn't possible to add meta data to an entity class such as when a model is automatically generated by an ORM tool. In this case any meta data you had applied would be lost.

Therefore, your original (automatically generated) class would be defined as a partial class:

public partial class Customer
{
    public string Priority { get; set; }
}

And then you would generate your buddy classes to add the meta data.

[MetadataType(typeof(CustomerMetaData))]
public partial class Customer
{
}

internal class CustomerMetaData
{
    [Required]
    public string Priority { get; set; }
}

You would then pass the Customer class to the view where the Priority would be set.

In your case i'm not sure if you only have one partial class or two (as the other is not shown but please provide if there is). I'm interested to know how you obtain the priority information from the customer as i'm wondering if this is an issue with how you use ModelState.IsValid? The reason I ask is that no set accessor is declared on the Priority property so i'm wondering how this is set from the view in order to report that it is not valid?



回答3:

You would also use a buddy class when it isn't possible to add meta data to an entity class such as when a model is automatically generated by an WCF Data Contract.