嵌套复杂类型显示名称 [英] Display name for nested complex type

查看:150
本文介绍了嵌套复杂类型显示名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个模型类象下面这样,

我想如果嵌套使用不同的标签Categoy.N​​ame财产,但如果我使用复杂类型,它没有绑定到name属性,是否有任何选项类似BindTo(Category.Name)的复杂类型? ??

 公共类产品型号
    {
        公众诠释编号{获得;组; }

        [DisplayName的(产品名称)]
        公共字符串名称{;组; }

        [DisplayName的(类别名称)]
        [BindTo(Category.Name)]
        公共CategoryModel分类{获取;组; }
    }

    公共类CategoryModel
    {
        公众诠释编号{获得;组; }
        [DisplayName的(名称)]
        公共字符串名称{;组; }
    }
 

解决方案

一种解决方案是使用<一个href="http://msdn.microsoft.com/en-us/library/system.componentmodel.dataannotations.metadatatypeattribute.aspx"相对=nofollow> MetadataType 属性。

 公共类产品型号
{
    公众诠释编号{获得;组; }

    [DisplayName的(产品名称)]
    公共字符串名称{;组; }

    公共CategoryPropertyModel分类{获取;组; }
}

[MetadataType(typeof运算(ICategoryModelCategoryDisplay))
公共类CategoryModel:CategoryBaseModel {}

[MetadataType(typeof运算(ICategoryModelDisplay))
公共类CategoryPropertyModel:CategoryBaseModel {}

公共类CategoryBaseModel
{
    公众诠释编号{获得;组; }
    公共字符串名称{;组; }
}

公共接口ICategoryModelSimpleDisplay
{
    [DisplayName的(名称)]
    公共字符串名称{;组; }
}

公共接口ICategoryModelCategoryDisplay
{
    [DisplayName的(类别名称)]
    公共字符串名称{;组; }
}
 

在使用 CategoryModel 的显示名称将是名称

在使用 CategoryPropertyModel 的显示名称将是类别名称

i have two model class like below,

i want to use different labels for Categoy.Name property if its nested,but if i use complex type,it didnt bind to name property,is there any options something like BindTo("Category.Name") for complex types???

  public class ProductModel
    {
        public int Id { get; set; }

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

        [DisplayName("Category Name")]
        [BindTo("Category.Name")]
        public CategoryModel Category { get; set; }
    }

    public class CategoryModel
    {
        public int Id { get; set; }
        [DisplayName("Name")]
        public string Name { get; set; }
    }

解决方案

One solution is to use the MetadataType attribute.

public class ProductModel
{
    public int Id { get; set; }

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

    public CategoryPropertyModel Category { get; set; }
}

[MetadataType(typeof(ICategoryModelCategoryDisplay))
public class CategoryModel : CategoryBaseModel { }

[MetadataType(typeof(ICategoryModelDisplay))
public class CategoryPropertyModel : CategoryBaseModel { }

public class CategoryBaseModel
{
    public int Id { get; set; }
    public string Name { get; set; }
}

public interface ICategoryModelSimpleDisplay  
{
    [DisplayName("Name")]
    public string Name { get; set; }
}

public interface ICategoryModelCategoryDisplay  
{
    [DisplayName("Category Name")]
    public string Name { get; set; }
}

When you use CategoryModel the display name will be Name.

When you use CategoryPropertyModel the display name will be Category Name.

这篇关于嵌套复杂类型显示名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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