ModelMetaData:如何获得"家长和QUOT; - 元数据?

问题描述:

在我看来,我叫Html.EditFor()触发该数据类型的自定义编辑模板。此外,我通过一些元数据与它(这就是我不喜欢的部分):

In my view I call Html.EditFor() which triggers a custom editor-template for this datatype. Additionally I pass some Metadata with it (and that's the part I don't like):

<% ModelMetadata metaTitle = ModelMetadataProviders.Current.GetMetadataForProperty(null, Model.GetType(), "Title"); %>
<%: Html.EditorFor(x => Model.Title, new { metaData = metaTitle })%>

传递类型(属性名称)的类型为'翻译'的。在自定义编辑器模板我要读从可视数据传递的元数据,以便使用它:

The passed type (property Title) is of type 'Translation'. Within the custom editor-template I have to read the passed metadata from the viewData in order to use it:

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<Translation>" %>
// {...}
if (ViewData["metaData"] != null)
    metaData = (ModelMetadata)ViewData["metaData"];

有没有一些方法,我可以直接在自定义编辑器模板中访问元数据?不幸的是,如果我叫编辑模板内的以下的,我不会得到相同的元数据对象(其中,例如信息,如果标题,属性是必需的或不丢失):

Is there some way I could access the metadata directly within the custom editor-template? Unfortunately, if I call the following within the editor-template, I won't get the same metadata-object (where for example the information if the Title-Property is required or not is missing):

ModelMetadata metaData = ModelMetadataProviders.Current.GetMetadataForType(null, Model.GetType());

我想,以避免通过在每次调用元数据对象...

I would like to avoid to pass the metadata-object on each call...

THX任何的窍门!
sl3dg3

Thx for any tipps! sl3dg3

我想你想获得的EditorTemplate实际的属性的元数据。

I think you're trying to get the metadata for the actual property for the EditorTemplate.

您可以做这样的(在EditorTemplate内):

You can do this like this (inside the EditorTemplate):

var metadata = ModelMetadata.FromStringExpression("", ViewData);

是指MVC当前属性。

"" means for MVC the current property.