MVC 3 - Accessing ModelMetaData for IEnumerable Model?
I'm trying to access the ModelMetaData for a Custom HtmlHelper that I'm working on. The HtmlHelper has a signature like so ...
public static MvcHtmlStrin开发者_JAVA百科g DataGrid<T>(this HtmlHelper<T> htmlHelper){}
The (Razor) View looks like this ...
@model IEnumerable<LogMyAssets.Models.ContactModel>
....
@Html.DataGrid()
My problem is that I can't access the ModelMetaData for the Model as it's IEnumerable. I thought I could do the following:
var model = (IEnumerable<T>)htmlHelper.ViewData.Model;
var metaData = model.ElementAt(0).GetMetadata();
public static ModelMetadata GetMetadata<TModel>(this TModel model)
{
return ModelMetadataProviders.Current.GetMetadataForType(null, typeof(TModel));
}
But stangely enough I get the following error:
Unable to cast object of type 'System.Collections.Generic.List`1[LMA.Models.ContactModel]'
to type 'System.Collections.Generic.IEnumerable`1[System.Collections.Generic.IEnumerable`1
I though I could could cast from Generic List to Generic IEnumerable. Am I missing something?
I don't really understand where the T
is defined in (IEnumerable<T>)
, however my Guess is that T
is already IEnumerable<something>
, which means you're trying to cast to IEnumerable<IEnumerable<something>>
精彩评论