如何获取 Razor 视图引擎中集合中项目的元数据?
How can I get the metadata of the items in a collection in razor view-engine?
我在 ASP.NET MVC 5 框架的顶部有一个用 C#
编写的项目。我试图将我的视图与我的视图模型分离,以便我可以重用我的视图。通过大量使用 EditorTemplates
,我能够通过评估 ModelMetadata
和每个 属性 的数据注释属性来创建我所有的标准视图(即创建、编辑和详细信息)在模型上,然后渲染页面。唯一令我困惑的是如何呈现 Index
视图。
我的索引视图通常接受 IEnumerable<object>
或 IPagedList<object>
集合。在我看来,我希望能够评估集合中每个 object
/记录的 ModelMetadata,以确定是否应显示 object
上的 属性。
换句话说,我的视图模型看起来像这样
public class DisplayPersonViewModel
{
public int Id{ get; set; }
[ShowOnIndexView]
public string FirstName { get; set; }
[ShowOnIndexView]
public string LastName { get; set; }
[ShowOnIndexView]
public int? Age { get; set; }
public string Gender { get; set; }
}
然后我的 Index.cshtml
视图将为集合中的每条记录接受 IPagedList<DisplayPersonViewModel>
,我想显示用 ShowOnIndexView
属性修饰的 属性 的值.
通常情况下,我可以使用类似这样的方法在我的视图中评估 ModelMetadata
@model IPagedList<object>
@{
var elements = ViewData.ModelMetadata.Properties.Where(metadata => !metadata.IsComplexType && !ViewData.TemplateInfo.Visited(metadata))
.OrderBy(x => x.Order)
.ToList();
}
<tr>
@foreach(var element in elements)
{
var onIndex = element.ContainerType.GetProperty(element.PropertyName)
.GetCustomAttributes(typeof(ShowOnIndexView), true)
.Select(x => x as ShowOnIndexView)
.FirstOrDefault(x => x != null);
if(onIndex == null)
{
continue;
}
@Html.Editor(element.PropertyName, "ReadOnly")
}
</tr>
然后我的控制器看起来像这样
public class PersonController : Controller
{
public ActionResult Index()
{
// This would be a call to a service to give me a collection with items. but I am but showing the I would pass a collection to my view
var viewModel = new List<DisplayPersonViewModel>();
return View(viewModel);
}
}
然而,为 IPagedList<DisplayPersonViewModel>
评估 ModelMetadata
的问题在于,它为我提供了关于集合本身的信息,而不是关于集合中的通用类型或单个模型的信息。换句话说,我得到的信息包括总页数、每页项目数、总记录数……
问题
如何访问集合中每一行的 ModelMetadata
信息,以便能够知道哪些 属性 显示,哪些不显示?
我将通过建议您不要使用此类代码污染您的视图来作为此答案的开头。一个更好的解决方案是创建自定义 HtmlHelper
扩展方法来生成 html,这为您提供了更大的灵活性,可以进行单元测试,并且可重用。
您需要更改的第一件事是视图中的模型声明
@model object
否则你会抛出(List<DisplayPersonViewModel>
不是IEnumerable<object>
或IPagedList<object>
,而是object
)
请注意,不清楚您是想要集合中 type
的 ModelMetadata
还是集合中每个项目的 ModelMetadata
,因此我将两者都包括在内,加上获取 type
@model object
@{
var elements = ViewData.ModelMetadata.Properties.Where(metadata => !metadata.IsComplexType && !ViewData.TemplateInfo.Visited(metadata)).OrderBy(x => x.Order).ToList();
// Get the metadata for the model
var collectionMetaData = ViewData.ModelMetadata;
// Get the collection type
Type type = collectionMetaData.Model.GetType();
// Validate its a collection and get the type in the collection
if (type.IsGenericType)
{
type = type.GetInterfaces().Where(t => t.IsGenericType)
.Where(t => t.GetGenericTypeDefinition() == typeof(IEnumerable<>))
.Single().GetGenericArguments()[0];
}
else if (type.IsArray)
{
type = type.GetElementType();
}
else
{
// its not a valid model
}
// Get the metadata for the type in the collection
ModelMetadata typeMetadata = ModelMetadataProviders.Current.GetMetadataForType(null, type);
}
....
@foreach (var element in collectionMetaData.Model as IEnumerable))
{
// Get the metadata for the element
ModelMetadata elementMetadata = ModelMetadataProviders.Current.GetMetadataForType(() => element, type);
....
请注意,使用反射来确定属性是否存在于循环的每次迭代中是低效的,我建议您这样做一次(基于 typeMetadata
)。然后,您可以(例如)生成一个 bool
值数组,然后在循环中使用索引器来检查该值)。更好的选择是让您的 ShowOnIndexView
属性实现 IMetadataAware
并向 ModelMetadata
的 AdditionalValues
添加一个值,这样根本不需要 var onIndex
代码.有关实施 IMetadataAware
的示例,请参阅 CustomAttribute reflects html attribute MVC5
我在 ASP.NET MVC 5 框架的顶部有一个用 C#
编写的项目。我试图将我的视图与我的视图模型分离,以便我可以重用我的视图。通过大量使用 EditorTemplates
,我能够通过评估 ModelMetadata
和每个 属性 的数据注释属性来创建我所有的标准视图(即创建、编辑和详细信息)在模型上,然后渲染页面。唯一令我困惑的是如何呈现 Index
视图。
我的索引视图通常接受 IEnumerable<object>
或 IPagedList<object>
集合。在我看来,我希望能够评估集合中每个 object
/记录的 ModelMetadata,以确定是否应显示 object
上的 属性。
换句话说,我的视图模型看起来像这样
public class DisplayPersonViewModel
{
public int Id{ get; set; }
[ShowOnIndexView]
public string FirstName { get; set; }
[ShowOnIndexView]
public string LastName { get; set; }
[ShowOnIndexView]
public int? Age { get; set; }
public string Gender { get; set; }
}
然后我的 Index.cshtml
视图将为集合中的每条记录接受 IPagedList<DisplayPersonViewModel>
,我想显示用 ShowOnIndexView
属性修饰的 属性 的值.
通常情况下,我可以使用类似这样的方法在我的视图中评估 ModelMetadata
@model IPagedList<object>
@{
var elements = ViewData.ModelMetadata.Properties.Where(metadata => !metadata.IsComplexType && !ViewData.TemplateInfo.Visited(metadata))
.OrderBy(x => x.Order)
.ToList();
}
<tr>
@foreach(var element in elements)
{
var onIndex = element.ContainerType.GetProperty(element.PropertyName)
.GetCustomAttributes(typeof(ShowOnIndexView), true)
.Select(x => x as ShowOnIndexView)
.FirstOrDefault(x => x != null);
if(onIndex == null)
{
continue;
}
@Html.Editor(element.PropertyName, "ReadOnly")
}
</tr>
然后我的控制器看起来像这样
public class PersonController : Controller
{
public ActionResult Index()
{
// This would be a call to a service to give me a collection with items. but I am but showing the I would pass a collection to my view
var viewModel = new List<DisplayPersonViewModel>();
return View(viewModel);
}
}
然而,为 IPagedList<DisplayPersonViewModel>
评估 ModelMetadata
的问题在于,它为我提供了关于集合本身的信息,而不是关于集合中的通用类型或单个模型的信息。换句话说,我得到的信息包括总页数、每页项目数、总记录数……
问题
如何访问集合中每一行的 ModelMetadata
信息,以便能够知道哪些 属性 显示,哪些不显示?
我将通过建议您不要使用此类代码污染您的视图来作为此答案的开头。一个更好的解决方案是创建自定义 HtmlHelper
扩展方法来生成 html,这为您提供了更大的灵活性,可以进行单元测试,并且可重用。
您需要更改的第一件事是视图中的模型声明
@model object
否则你会抛出List<DisplayPersonViewModel>
不是IEnumerable<object>
或IPagedList<object>
,而是object
)
请注意,不清楚您是想要集合中 type
的 ModelMetadata
还是集合中每个项目的 ModelMetadata
,因此我将两者都包括在内,加上获取 type
@model object
@{
var elements = ViewData.ModelMetadata.Properties.Where(metadata => !metadata.IsComplexType && !ViewData.TemplateInfo.Visited(metadata)).OrderBy(x => x.Order).ToList();
// Get the metadata for the model
var collectionMetaData = ViewData.ModelMetadata;
// Get the collection type
Type type = collectionMetaData.Model.GetType();
// Validate its a collection and get the type in the collection
if (type.IsGenericType)
{
type = type.GetInterfaces().Where(t => t.IsGenericType)
.Where(t => t.GetGenericTypeDefinition() == typeof(IEnumerable<>))
.Single().GetGenericArguments()[0];
}
else if (type.IsArray)
{
type = type.GetElementType();
}
else
{
// its not a valid model
}
// Get the metadata for the type in the collection
ModelMetadata typeMetadata = ModelMetadataProviders.Current.GetMetadataForType(null, type);
}
....
@foreach (var element in collectionMetaData.Model as IEnumerable))
{
// Get the metadata for the element
ModelMetadata elementMetadata = ModelMetadataProviders.Current.GetMetadataForType(() => element, type);
....
请注意,使用反射来确定属性是否存在于循环的每次迭代中是低效的,我建议您这样做一次(基于 typeMetadata
)。然后,您可以(例如)生成一个 bool
值数组,然后在循环中使用索引器来检查该值)。更好的选择是让您的 ShowOnIndexView
属性实现 IMetadataAware
并向 ModelMetadata
的 AdditionalValues
添加一个值,这样根本不需要 var onIndex
代码.有关实施 IMetadataAware
的示例,请参阅 CustomAttribute reflects html attribute MVC5