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
.