2012-07-02 16 views
5

पर प्रदर्शन एट्रिब्यूट का उपयोग करें क्योंकि हम संपत्ति के लिए लेबल सेट करने के लिए System.ComponentModel.DataAnnotations.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

धन्यवाद फिल, यह मेरा पहला प्रश्न है। क्या आप दृश्य में मूल्य पुनर्प्राप्त करने का एक आसान तरीका जानते हैं (मैंने अपना पहला संदेश संपादित किया है)? मुझे पता है कि मैं रिफ्लेक्सन का उपयोग कर सकता हूं, लेकिन उदाहरण के लिए एचटीएमएलहेल्पर की तरह एक आसान तरीका है? दुर्भाग्यवश –

+0

@DudePascalou नहीं। मैंने '(मॉडल => मॉडल) 'के विविधता के साथ' Html.LabelFor' 'के साथ कुछ त्वरित खेल किया है, लेकिन कहीं भी नहीं मिला :-( – PhilPursglove

1

मैं वास्तव में अगर यह एक और तरीका यह है है पता नहीं है, लेकिन मैं आम तौर पर करने के लिए नहीं hard code इस मैं ध्यान में रखते हुए एक चर बनाने का उपयोग और उसके बाद मैं बुलाया मैं जहां की जरूरत है। आपके मामले में यह एक छोटे और अधिक सुरुचिपूर्ण मैं

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

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

उत्तर के लिए धन्यवाद। मैं अंतिम उपाय पर रिफ्लेक्सन द्वारा DisplayNameAttribute मान पुनर्प्राप्त करूंगा , यदि कोई आसान तरीका नहीं है। –

1

करूँगा डेकोरेटर पैटर्न का उपयोग करने के लिए, बस अपने स्वयं के कस्टम के साथ DisplayAttribute लपेट वर्गों के लिए विशेष रूप से श्रेय दें।

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

Tks @Blake, मुझे लगता है कि यह चाल करेगा। क्या आप हमें दिखा सकते हैं कि यह दृश्य में कैसा दिखता है? क्या एक '@ HTML.LabelFor' पर्याप्त होगा? –