我正在尝试访问我正在处理的Custom HtmlHelper的ModelMetaData。 HtmlHelper有这样的签名......
public static MvcHtmlString DataGrid<T>(this HtmlHelper<T> htmlHelper){}
(Razor)视图看起来像这样......
@model IEnumerable<LogMyAssets.Models.ContactModel>
....
@Html.DataGrid()
我的问题是我无法访问模型的ModelMetaData,因为它是IEnumerable。我以为我 可以做到以下几点:
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));
}
但是我很容易得到以下错误:
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
我虽然可以从Generic List转换为Generic IEnumerable。我错过了什么吗?
答案 0 :(得分:0)
我真的不明白T
中(IEnumerable<T>)
的定义,但我的猜测是T
已经IEnumerable<something>
,这意味着你正试图转为IEnumerable<IEnumerable<something>>