我有我自己的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