开发者

Data Annotations in MC2 - Issue with base class validation

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 Descri开发者_运维百科ption { 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!


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜