3

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 MvcHtmlString 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?

tereško
  • 58,060
  • 25
  • 98
  • 150
Click Ahead
  • 2,782
  • 6
  • 35
  • 60

1 Answers1

0

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>>

Danny Tuppeny
  • 40,147
  • 24
  • 151
  • 275