[ASP.MVC] 在Controller抓Metadata的DisplayName

摘要:[ASP.MVC] 在Controller抓Metadata的DisplayName

看了 ASP.NET MVC 資料分頁 MVCPaging 2.0 應用 Part.4:分頁進階處理

http://kevintsengtw.blogspot.tw/2012/07/aspnet-mvc-mvcpaging-20-part4.html?showComment=1359514243247#c234738590850487369

這篇文章後想說如果可以在Controller抓Metadata的DisplayName
就會更方便在專案運用上

於是我找到下列資料可以參考
Using an Associated Metadata Class outside Dynamic Data

http://blogs.msdn.com/b/davidebb/archive/2009/07/24/using-an-associated-metadata-class-outside-dynamic-data.aspx
 
參考

			TypeDescriptor.AddProvider(
				new AssociatedMetadataTypeTypeDescriptionProvider(typeof(System_Admins)),
				typeof(System_Admins));

			// Get the property descriptor for UnitsInStock
			PropertyDescriptor propDesc = TypeDescriptor.GetProperties(
				typeof(System_Admins)).Find(columnName, true);

			// Get the display name attribute, which is not actually on the property,
			// but on its counterpart in the 'buddy' class
			var displayName = propDesc.Attributes.OfType().First();
			string DisplayName = displayName.DisplayName;