我試圖通過使用OOP爲我的視圖模型來acheive DRY原則:忽略「顯示」剃刀中的屬性查看
public class ItemBase
{
[Display(Name = "Description - Displayed in the list (so make this meaningful)")]
[Required]
public string Description { get; set; }
[Display(Name = "Username")]
[Required]
public string UserName { get; set; }
[Display(Name = "Optional Secondary Credential")]
public string SecondCredential { get; set; }
[Display(Name = "Location - Ideally a URL")]
[Required]
public string Location { get; set; }
[Display(Name = "Additional Notes")]
public string Notes { get; set; }
}
public class ItemDisplay : ItemBase
{
[Required]
public Int32 ItemId { get; set; }
public ApplicationUser Creator { get; set; }
}
public class ItemEdit : ItemBase
{
[Required]
public Int32 ItemId { get; set; }
}
public class ItemAdd : Itemase
{
[Required]
public Int32 Parent_CategoryId { get; set; }
}
這不工作的偉大...然而,當我想用ItemDisplay類在視圖中顯示數據,它顯示Display屬性數據。如果不將所有成員放入沒有顯示attrbute的ItemDisplay類,我將如何讓視圖忽略這些視圖?
查看代碼
<article class="first">
<h2>Details</h2>
<div class="form-horizontal toppaddding">
<div class="form-group">
@Html.LabelFor(model => model.ViewItem.Description, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.Raw(Model.ViewItem.Description)
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.ViewItem.UserName, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.Raw(Model.ViewItem.UserName)
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.ViewItem.SecondCredential, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.Raw(Model.ViewItem.SecondCredential)
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.ViewItem.Password, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.Raw(Model.ViewItem.Password)
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.ViewItem.Location, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.Raw(Model.ViewItem.Location)
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.ViewItem.Notes, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.Raw(Model.ViewItem.Notes)
</div>
</div>
</div>
</article>
你能告訴你的看法標記?我不太明白你要求的是什麼 – Tommy 2014-10-06 11:45:49
只需使用不同的HTML助手。例如,如果您使用'Html.DisplayFor',請嘗試'Html.Display'。 – DavidG 2014-10-06 11:47:01
@DavidG - 是的,這就是爲什麼我想看到他的看法。可以通過不調用顯示它的HTML助手來忽略Display屬性。 :) – Tommy 2014-10-06 11:54:31