将数据注释应用于MVC中的视图模型的子属性? [英] Applying Data Annotations to sub properties of the View Model in MVC?

查看:57
本文介绍了将数据注释应用于MVC中的视图模型的子属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在属性上放置简单的数据注释很棒,

Putting simple Data Annotations on properties is great,

public class UnicornViewModel
{
   [Required]
   public string Name { get; set; }

但是可以说我有这样的东西:

But lets say I'm have something like this:

public class SuperPower
{
   public class Name { get; set; }
}

public class UnicornViewModel
{
   [Required]
   public string Name { get; set; }

   public SuperPower PrimarySuperPower { get; set; }

   public SuperPower SecondarySuperPower { get; set; }

如何在PrimarySuperPower.Name上应用Required属性,而对SecondarySuperPower.Name保留可选属性?最好是1.与客户端验证相关联的东西; 2.无需任何特殊处理,例如检查操作/自定义验证器中PrimarySuperPower.Name的值,并添加ModelState错误(如果为空)。

How do I apply the Required attribute on PrimarySuperPower.Name while leaving it optional for SecondarySuperPower.Name? Preferably 1. something that ties into client side validation and 2. with out any special handling like checking the value of PrimarySuperPower.Name in the Action/Custom validator and add a ModelState error if it's empty. It would be great if there was something like:

   [Required(p => p.Name)]
   public SuperPower PrimarySuperPower { get; set; }

   public SuperPower SecondarySuperPower { get; set; }


推荐答案

这可能是一个较晚的答案,但我发现寻找相同事物时的这个问题。
这就是我解决特殊情况的方法:

This might be a late answer, but I found this question when searching for the same thing. This is how I solved my particular situation:

在我遇到这个问题之前:

Before I had this:

public class ProductVm
{
    //+ some other properties        

    public Category Category {get; set;}
    public Category ParentCategory {get; set;}
}

我想要一些类似的东西:

For which I wanted to have something in the likes of:

public class ProductVm
{
    //some other properties        

    [DisplayName("Product Category", e => e.Description)]
    public Category Category {get; set;}
    [DisplayName("Parent Category", e => e.Description)]
    public Category ParentCategory {get; set;}
}

我无法在模型本身中输入此内容,因为两者都是

I couldn't enter this in the model itself since both are the same object class.

我这样解决了它(因为在这种情况下,我只需要读取Description值而不是将其写入):

I solved it like this (since I only needed to read the Description value in this case and not write it):

public class ProductVm
{
    //some other properties        

    public Category Category {get; set;}
    public Category ParentCategory {get; set;}

    [DisplayName("Product Category")]
    public string Category => Category.Description;

    [DisplayName("Main Category")]
    public string ParentCategory => ParentCategory.Description;
}

您可以将其重写一些,以保留剩余的私有支持字段并删除类别对象的属性封装,但就我而言,我仍然需要将它们公开以用于其他用途。

You could possible just rewrite it a bit more to keep the remaining private backing fields and remove the property encapsulation of the Category objects, but in my case I still needed them to be public for other uses.

关于上述问题,我将执行以下操作:

Concerning the above question I would do the following:

public class UnicornViewModel
{
    [Required]
    public string Name { get; set; }

    public SuperPower PrimarySuperPower { get; set; }

    public SuperPower SecondarySuperPower { get; set; }

    [Required]
    public string PrimarySuperPowerName 
    {
        get { return PrimarySuperPower.Name; }
        set { PrimarySuperPower.Name = value; }
    }

    public string SecondarySuperPowerName 
    {
        get { return SecondarySuperPower.Name; }
        set { SecondarySuperPower.Name = value; }
    }
}

然后将View绑定到字符串属性并排除SuperPower属性。

And then I'd bind my View to the string properties and exclude the SuperPower properties.

这篇关于将数据注释应用于MVC中的视图模型的子属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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