Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
I have custom html helper which getting IEnumerable model from view and generates html table with headers and body
please advice how can get matadata from this model
thanks
To get properties:
YourModel.GetType().GetProperties();
To get the name of a property:
property.Name;
To get the value of a property:
var result = YourModel.GetType().InvokeMember("NameOfProperty", BindingFlags.GetProperty, null, YourModel, null);
Some more well developed examples can be found in this file on GitHub: https://github.com/jamestharpe/Rolcore/blob/master/src/Rolcore/Reflection/ObjectExtensions.cs
If I'm understanding what you are trying to do correctly the below should work with minimal reflection:
public static MvcHtmlString MakeTable<TModel, TValue>(this HtmlHelper<TModel> html, IEnumerable<TValue> table) { var modelMetaData = ModelMetadataProviders.Current.GetMetadataForType(null, typeof(TValue)); foreach (TValue row in table) { //write out table } }
获取属性:
要获取属性的名称:
要获取属性的值:
可以在 GitHub 上的此文件中找到一些更完善的示例:https ://github.com/jamestharpe/Rolcore/blob/master/src/Rolcore/Reflection/ObjectExtensions.cs