views:

148

answers:

1

I have an Entity Framework 4 model first layer, where I have a Product base class and then different classes that derive from the class. For each type of Product there is a Partial Class, and then each of the partial classes uses a buddy class for Data Annotations.

[MetadataType(typeof(Product_Validation))]
public partial class Product : EntityObject
{
    private List<RuleViolation> ruleViolations = new List<RuleViolation>();
}

and then

public  class Product_Validation
{


    [DisplayName("Model Name")]
    public string ModelName { get; set; }

    [Required(ErrorMessage = "A description is required")]
    [StringLength(2, ErrorMessage = "must be 2 or more")]
    public string Description { get; set; }
}

The issue I have run into is that the base class validation (for product) is not happening server side in my ASP.NET MVC2 application. I have not tried client side yet.

The classes that are derived from Product, such as CD, do have the validation working. I know the wiring of the buddy class is working for the base product class because the Display Names I am using are being utilized. When I do my binding for the model I am using the derived class (CD).

Any ideas?

I looked at this SO Post but it did not seem to get me going.

Thanks!

+1  A: 

Are you model binding the base or derived class?

If I remember correctly from some experimentation months ago you need to bind as a Product. You cannot use CD's in your action methods or UpdateModel calls.

You may have to do two sets of model binding to make this work right. Once for Product validation and another for CD.

jfar
If that is true, I would pin this as a good candidate for another question and possibly a feature request.
NickLarsen
I am binding the derived class. How does that work for a view then if I want both base Product and derived CD? I have a strongly typed view that inherits a viewmodel that includes the derived class (CD).Thanks
John Ptacek
@John Ptacek I have no idea what your asking. If you have a view with a CD model, you also have a view with a Product Model.
jfar
@NickLarsen Sorry... Kinda new to this MVC thing. I have a view model which has a derived class (CD), pagination and a filter. This is the model that my view inherits. When the forms posts back to my Action this is the view model that I validate against. Do I need to cast the derived class (CD) to the base class (product) to validate against the base class model? Apologize if I am getting the lingo incorrect
John Ptacek
@John Ptacek Casting won't fix your issue. Internally the ModelBinder calls GetType() which always returns the most specific type.
jfar