我有以下代碼塊在剃刀視圖內複製並粘貼了大約5次不同的時間。它基本上只顯示不同數據的同一模型的表格。Html Helper在mvc3剃刀視圖中創建可重用的func?
我該如何將它重寫爲html助手或lambda func,以便可以將它重用到n個傳入視圖的不同模型中?
// Example for Model.A and Model.B
var cCols = new[] { "val1", "val2"};
// Display the data for A
<div class="group-property">
<div class="group-label">Title A</div>
<table class="collection-table">
<thead>
<tr class="collection-head">@foreach (var col in cCols) {<th scope="col">@col</th>}</tr>
</thead>
<tbody>
@foreach (var item in Model.A)
{
<td>@item.val1</td>
<td>@item.val2</td>
}
</tbody>
</table>
</div>
// Display the data for B
<div class="group-property">
<div class="group-label">Title B</div>
<table class="collection-table">
<thead>
<tr class="collection-head">@foreach (var col in cCols) {<th scope="col">@col</th>}</tr>
</thead>
<tbody>
@foreach (var item in Model.B)
{
<td>@item.val1</td>
<td>@item.val2</td>
}
</tbody>
</table>
</div>
太棒了!謝謝! – JaJ