我使用的是帶有EntityFramework 6 DataAnnotations的asp.net MVC 5。 我想知道是否有辦法讓一個對象的所有DisplayName
並將它們保存在一個變量到一個控制器類。獲取DisplayName屬性的所有值
例如,考慮到類:
public class Class1
{
[DisplayName("The ID number")]
public int Id { get; set; }
[DisplayName("My Value")]
public int Value { get; set; }
[DisplayName("Label name to display")]
public string Label { get; set; }
}
如何獲得的DisplayName
值的所有屬性?例如如何創建一個返回Dictionary< string,string >
它與屬性名稱和價值的關鍵與DisplayName
的功能,如:
{ "Id": "The ID name", "Value": "My Value", "Label": "Label name to display"}.
我已經看到了這個話題stackoverflow - get the value of DisplayName attribute但我有沒有辦法想法,擴展了這一碼。
'DisplayName'或'Display'(它們是不同的)? –
@IvanStoev'DisplayName' – Cyr