ASP.NET MVC DisplayAttribute和接口 [英] ASP.NET MVC DisplayAttribute and interfaces

查看:152
本文介绍了ASP.NET MVC DisplayAttribute和接口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些接口和类

public inteface IRole
{
  int Id { get; }
  string Name { get; set; }
}

public class Role : IRole
{
  public int Id { get; }
  [Display("Role Name")]
  public string Name { get; set; }
}

public class Member 
{
  [Display("Login")]
  public string Login { get; set; }
  [Display("Password")]
  public string Password { get; set; }
  public IRole Role { get; set; }
}

在查看我尝试使用,查看强烈会员类型

on View I try to use, View is strongly type of Member

在这条线从DisplayAttribute显示正确的消息

on this line displays correct message from DisplayAttribute

<%= Html.LabelFor(m => m.Login) %>

在这条线不会从DisplayAttribute显示正确的标签

on this line it does not display correct label from DisplayAttribute

<%= Html.LabelFor(m => m.Role.Name) %>

我怎样才能解决这个问题有正确的标签在这样的架构?

How can I fix this to have correct labels in such architecture?

感谢

P.S。我知道有关添加DisplayAttribute到接口领域,一切都将正常工作,但也许有不同的解决方案。

P.S. I know about adding DisplayAttribute to the interface field and all will work, but maybe there are different solution.

推荐答案

一切工作的设计在这里。

Everything is working as designed here.

您已经知道答案。如果您对IRole显示一个表单,你还必须在IRole的属性。

You already know your answer. If you display a form for IRole you must also have the attributes on IRole.

为了让你不得不实现自己的TypeDescriptors或ModelMetadataProvider正确的标签,以斯马什·起来的元数据接口为实现它们的具体类。

In order to have the correct labels you'd have to implement your own TypeDescriptors or ModelMetadataProvider in order to "smush" together the metadata for an interface into any concrete classes that implement them.

这将成为非常复杂的实快。

This will become really complex real fast.

为什么你不能只是属性添加到IRole接口?

Why can't you just add the attribute to the IRole interface?

这篇关于ASP.NET MVC DisplayAttribute和接口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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