2012-07-02 4 views
5

System.ComponentModel.DataAnnotations.DisplayAttribute을 사용하여 속성 레이블을 설정 했으므로 클래스에 사용할 수 있지만 클래스에는 사용할 수 없습니다.클래스에 DisplayAttribute 사용

using System.ComponentModel.DataAnnotations; 

[Display(Name = "A person")] 
public class Person 
{ 
    [Display(Name = "A name")] 
    public string Name { get; set; } 
} 

누구나 해결 방법을 알고 있습니까?

편집 : 강력한 형식의보기에서 사용하고 싶습니다. 나는 새로운 강력한 형식의 뷰를 만들 때 클래스 이름은 하드처럼, HTML로 코딩되어

@model Models.Person 
<fieldset> 
    <legend>Person</legend> 
    <div class="display-label"> 
     @Html.LabelFor(model => model.Name) 
    </div> 
</fieldset> 

내가 Name 재산에 비슷한 일을하고 싶다.

+8

을 당신이하고 싶은 이유는? 다음과 같이

using System; using System.ComponentModel.DataAnnotations; namespace YourNameSpaceHere.Support { [AttributeUsage(AttributeTargets.Class)] public class DisplayForClassAttribute : Attribute { protected readonly DisplayAttribute Attribute; public DisplayForClassAttribute() { this.Attribute = new DisplayAttribute(); } public string ShortName { get { return this.Attribute.ShortName; } set { this.Attribute.ShortName = value; } } public string Name { get { return this.Attribute.Name; } set { this.Attribute.Name = value; } } public string Description { get { return this.Attribute.Description; } set { this.Attribute.Description = value; } } public string Prompt { get { return this.Attribute.Prompt; } set { this.Attribute.Prompt = value; } } public string GroupName { get { return this.Attribute.GroupName; } set { this.Attribute.GroupName = value; } } public Type ResourceType { get { return this.Attribute.ResourceType; } set { this.Attribute.ResourceType = value; } } public bool AutoGenerateField { get { return this.Attribute.AutoGenerateField; } set { this.Attribute.AutoGenerateField = value; } } public bool AutoGenerateFilter { get { return this.Attribute.AutoGenerateFilter; } set { this.Attribute.AutoGenerateFilter = value; } } public int Order { get { return this.Attribute.Order; } set { this.Attribute.Order = value; } } public string GetShortName() { return this.Attribute.GetShortName(); } public string GetName() { return this.Attribute.GetName(); } public string GetDescription() { return this.Attribute.GetDescription(); } public string GetPrompt() { return this.Attribute.GetPrompt(); } public string GetGroupName() { return this.Attribute.GetGroupName(); } public bool? GetAutoGenerateField() { return this.Attribute.GetAutoGenerateField(); } public bool? GetAutoGenerateFilter() { return this.Attribute.GetAutoGenerateFilter(); } public int? GetOrder() { return this.Attribute.GetOrder(); } } } 

사용이 될 것이다 – Jorge

+1

당신은 자신의 속성을 만들 수 있습니다. – MBen

답변

6

DisplayName 속성 (System.ComponentModel)은 비슷한 기능을 수행하며 클래스에 적용 할 수 있습니다.

MSDN

+0

감사합니다 필, 첫 번째 질문에 대한 대답. 보기에서 값을 검색하는 간단한 방법을 알고 있습니까 (첫 번째 메시지를 편집 했습니까?). 나는 reflexion을 사용할 수 있다는 것을 알고 있지만 HtmlHelper와 같은 간단한 방법이 있는가? –

+0

@DudePascalou 불행히도. 방금'Html.LabelFor'로'(model => model)'의 변형을 가지고 놀았지만 어디에도 가지 않았다 :-( – PhilPursglove

1

내가 정말이 작업을 수행하는 또 다른 방법이 있는지 모르겠지만 난 일반적으로하지 hard code이 내가보기에 변수를 만들어 사용하고 내가 필요한 경우 다음 내가했다. 귀하의 경우에는 단지 클래스를 위해 특별히 속성 사용자 정의로 DisplayAttribute 포장, 조금 더 우아한 내가 데코레이터 패턴을 사용하여

@{ 
    var viewName = typeof(Foo).Name; 
} 

@model Models.Person 
<fieldset> 
<legend>@viewName</legend> 
<div class="display-label"> 
    @Html.LabelFor(model => model.Name) 
</div> 
</fieldset> 
+0

답을 주셔서 감사합니다. 최후의 수단으로 Reflectionion에 의해 DisplayNameAttribute 값을 검색 할 것입니다. , 더 간단한 방법이 없다면 –

1

할 수 있습니다 할 수 있습니다.

[DisplayForClass(Name = "Approval Matrix")] 
public class ApprovalMatrixViewModel 
{ 
} 
+0

Tks @Blake, 트릭을 수행 할 것입니다.이 뷰가 어떻게 생겼는지를 보여줄 수 있습니까?'@Html.LabelFor'로 충분할 수 있습니까? –

관련 문제