2017-10-12 21 views
0

我的目標是創建許多類似的視圖,這些視圖僅在它們出現的字段列表中有所不同。例如,許多爲不同的模型和不同的字段組創建視圖。下面是一個片段從腳手架Create.cshtml頁:用於包裝模型屬性的剃刀頁部件的單點維護

<div class="form-group"> 
    @Html.LabelFor(model => model.NameText, htmlAttributes: new { @class = "control-label col-md-2" }) 
    <div class="col-md-10"> 
     @Html.EditorFor(model => model.NameText, new { htmlAttributes = new { @class = "form-control" } }) 
     @Html.ValidationMessageFor(model => model.NameText, "", new { @class = "text-danger" }) 
    </div> 
</div> 

<div class="form-group"> 
    @Html.LabelFor(model => model.NameDefinition, htmlAttributes: new { @class = "control-label col-md-2" }) 
    <div class="col-md-10"> 
     @Html.EditorFor(model => model.NameDefinition, new { htmlAttributes = new { @class = "form-control" } }) 
     @Html.ValidationMessageFor(model => model.NameDefinition, "", new { @class = "text-danger" }) 
    </div> 
</div> 

<div class ="form-group"> 
    @Html.LabelFor(model => model.NameComment, htmlAttributes: new { @class = "control-label col-md-2" }) 
    <div class="col-md-10"> 
     @Html.EditorFor(model => model.NameComment, new { htmlAttributes = new { @class = "form-control" } }) 
     @Html.ValidationMessageFor(model => model.NameComment, "", new { @class = "text-danger" }) 
    </div> 
</div> 

我的理解是,部分景觀可以添加上面,下面和<div class="form-group"> . . . </div>代碼塊中。但我不知道它能夠使用對模型屬性的引用(例如,NameText),然後用代碼塊包裝它。

另外我的理解是,@Helper指令已從剃刀(MVC 6)中刪除,並沒有給出任何簡單的替換。

我已經放置先於上面的片段它後面成一個單一的局部視圖的代碼的部分的代碼的一部分。通過一個小竅門,代碼的兩部分被合併到上面的代碼片段中,然後所有內容都被髮送到瀏覽器。但我需要的不僅僅是這些。 如何避免上面代碼片段中重複的代碼?

我想什麼有:關於向字段列表

,只會不同
  • 許多相似的看法
  • 強類型
  • 維護代碼的
  • 單點是很常見的許多意見

而作爲一個方面說明:我應該怎麼搜索,以便自己找到答案?

回答