1

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

4

2 回答 2

1

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
    }
}
于 2012-09-21T14:21:34.273 回答
-1

获取属性:

YourModel.GetType().GetProperties();

要获取属性的名称:

property.Name;

要获取属性的值:

var result = YourModel.GetType().InvokeMember("NameOfProperty", BindingFlags.GetProperty, null, YourModel, null);

可以在 GitHub 上的此文件中找到一些更完善的示例:https ://github.com/jamestharpe/Rolcore/blob/master/src/Rolcore/Reflection/ObjectExtensions.cs

于 2012-09-21T14:14:14.807 回答