2014-02-18 81 views
3

我可以枚舉剃刀模型中的列表嗎?在MVC剃鬚刀中顯示模型列表

我的模型:

public class PreapprovalViewModel 
{ 
    public int ProjectId { get; set; } 
    public int Decision { get; set; } 
    public List<BranchHead> BranchHeads { get; set; } 
    public List<SelectListItem> Decisions { get; set; } 
} 

論預先批准的指數我希望有一個表中顯示了BranchHead列表內的一些領域。

剃刀:

@model Mars.Intranet.ViewModels.PreapprovalViewModel 
@{ 
    ViewBag.Title = "Index"; 
} 
@Html.Partial("_ProjectPartial") 
<h2>Preapproval</h2> 
@using (Html.BeginForm()) { 
    <div> 
     Approve research request: 
     @Html.DropDownListFor(o => o.Decision, Model.Decisions) 
    </div> 
    <div id="assign-branch-head" style="display: none;"> 
     Assign branch heads 
     <table> 
      <tr> 
       <th>@Html.DisplayFor(o => o.BranchHeads.BranchName</th> <!-- Can I access the list in the model like this?--> 
       <th>@Html.DisplayFor(o => p.BranchHeads.FirstName</th> <!-- Can I access the list in the model like this?--> 
       <th>@Html.DisplayFor(o => p.BranchHeads.Comment</th> <!-- Can I access the list in the model like this?--> 
      </tr> 
      <tr> 
       <td><!-- I want the info from the list in model here--></td> 
       <td><!-- I want the info from the list in model here--></td> 
       <td><!-- I want the info from the list in model here--></td> 
      </tr> 
     </table> 
    </div> 
    <div class="approval-comment"> 
     Comment: 
     @Html.TextAreaFor(o => o.Comment) 
    </div> 
    <button type="submit" class="btn_submit"></button> 
} 

回答

7

是的,這是完全可能的。但由於這是一個List<T>,您需要遍歷它才能顯示數據。

只是包裝在foreach循環

@foreach (var item in Model.BranchHeads) 
{ 
    <tr> 
     <td> 
      @Html.DisplayFor(modelItem => item.BranchName)<br/>      
     </td> 
     <td> 
      @Html.DisplayFor(modelItem => item.FirstName)<br/>      
     </td> 
     <td>     
      @Html.DisplayFor(modelItem => item.Comment)<br/> 
     </td> 

    </tr> 
} 

此外你的數據,你可能需要使用Html.DisplayNameFor()顯示錶頭。

+0

是的,這看起來就像我需要的東西。謝謝 – Carel