MC2 中的数据注释 - 基类验证问题

发布于 2024-09-07 14:39:45 字数 964 浏览 2 评论 0原文

我有一个 Entity Framework 4 模型第一层,其中有一个 Product 基类,然后是从该类派生的不同类。对于每种类型的产品,都有一个部分类,然后每个部分类都使用一个伙伴类来进行数据注释。

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

然后

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

我遇到的问题是基类验证(针对产品)没有在我的 ASP.NET MVC2 应用程序的服务器端发生。我还没有尝试过客户端。

从 Product 派生的类(例如 CD)确实可以进行验证。我知道伙伴类的接线适用于基本产品类,因为我正在使用的显示名称正在被利用。当我对模型进行绑定时,我使用的是派生类 (CD)。

有什么想法吗?

我看了这篇SO Post,但它似乎并没有让我继续下去。

谢谢!

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

朕就是辣么酷 2024-09-14 14:39:45

您是否模型绑定基类或派生类?

如果我没记错的话,几个月前的一些实验你需要绑定为产品。您不能在操作方法或 UpdateModel 调用中使用 CD。

您可能需要进行两组模型绑定才能正常工作。一次用于产品验证,另一次用于 CD。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文