2014-10-06 33 views
1

我試圖通過使用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> 
+0

你能告訴你的看法標記?我不太明白你要求的是什麼 – Tommy 2014-10-06 11:45:49

+1

只需使用不同的HTML助手。例如,如果您使用'Html.DisplayFor',請嘗試'Html.Display'。 – DavidG 2014-10-06 11:47:01

+1

@DavidG - 是的,這就是爲什麼我想看到他的看法。可以通過不調用顯示它的HTML助手來忽略Display屬性。 :) – Tommy 2014-10-06 11:54:31

回答

0

Html.LabelFor幫手首先查找顯示dataAnnotation,如果沒有一個,它會默認爲輸出中的類屬性的名稱。

如果您不想在此特定視圖中使用顯示數據註釋,請不要使用Html.LabelFor幫助程序,而是要麼創建自己的標籤標籤,要麼在<div>之前輸出任何您想要的內容模型數據。

<div class="form-group"> 
      <label class = "control-label col-md-2">Alternate Description Label Text</label> 
      <div class="col-md-10"> 
       @Html.Raw(Model.ViewItem.Description) 
      </div> 
     </div> 

在這種情況下,您並未創建輸入表單,因此您可以考慮使用跨度而不是標籤。

+0

我確實想使用LabelFor ..或別的東西。我只想忽略[Display()]屬性。 – binks 2014-10-06 18:27:48

0

可以使用LabelFor重載接受labelText參數

@Html.LabelFor(model => model.ViewItem.UserName, 
       Model.ViewItem.Description, 
       htmlAttributes: new { @class = "control-label col-md-2" })