我有我自己的metadataprovider实现,在其中检查我的自定义属性并从数据库获取元数据。MVC metatdataprovider和html助手
public class EntityPropertyMetadataAttribute: Attribute
{
[MaxLength(256)]
public string EntityFullName { get; set; }
[MaxLength(64)]
public string PropertyName { get; set; }
public string DisplayName { get; set; }
public string DisplayFormatString { get; set; }
public string EditFormatString { get; set; }
public object DefaultValue { get; set; }
}
现在我发现以下的,如果我有这样一个观点:
<div class="editor-label">
@Html.LabelFor(model => model.Id)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Id)
@Html.ValidationMessageFor(model => model.Id)
</div>
功能
protected override ModelMetadata CreateMetadata(IEnumerable<Attribute> attributes, Type containerType, Func<object> modelAccessor, Type modelType, string propertyName)
被调用3次,物业编号,如果我删除一个,例如:
<div class="editor-field">
@Html.EditorFor(model => model.Id)
@Html.ValidationMessageFor(model => model.Id)
</div>
它g ets被称为2次。 现在,当我使用这个:
@using (Html.BeginForm()) {
@Html.ValidationSummary(true)
@Html.EditorForModel(Model)
<p>
<input type="submit" value="Create" />
</p>
}
CreateMetadata被调用模型中的每个属性高达22。 这不是你想要的性能明智之举。可能我应该把DB代码(目前在CreateMetadata方法中)连接到别的地方。
任何建议赞赏。
欢呼
你对自己的元数据提供者有什么作用? – jgauffin 2012-03-15 14:40:27
因此,我可以为具有不同角色的用户设置不同的“设置”。 – Jamee 2012-03-15 14:45:50
不同的用户在查看数据时取决于他们的组不同的设置。 – Jamee 2012-03-16 08:23:20