如何在ASP.NET MVC Razor项目的视图中显示集合?

问题描述:

如何在ASP.NET MVC Razor项目的视图中显示集合?

How to display a collection in View of ASP.NET MVC Razor project?

我的模型和视图在下面.对于一个人,我必须在屏幕上显示许多测试.在此先感谢

My Model and View is below. For one person i've to display Many tests on the screen. Thanks in Advance

namespace UI.Models
{
public class SuperStudent
{      
    public string FullName { get; set; }      

    public ICollection<TestDetail> CandidateTestDetails { get; set; }
}
public class TestDetail
{
    public DateTime TestDate { get; set; }
    public string RegNum { get; set; }     
}

}

查看

@model IEnumerable<UI.Models.SuperStudent>
   <p>
      @Html.ActionLink("Create New", "Create")
    </p>
    <table class="table">
    <tr>       
        <th>
            @Html.DisplayNameFor(model => model.FullName)
        </th>     
    </tr>
    @foreach (var item in Model) {
     <tr>
        <td>
         Print all test details            
        </td>
    </tr>
    }
     </table>

对于一个人,您的观点必须像:

For one person your view must be like :

@model UI.Models.SuperStudent
<h2>@Model.FullName</h2>
<table class="table">
<tr>       
    <th>
       TestDate 
    </th>  
   <th>
       RegNum
    </th>   
</tr>
@foreach (var item in Model.CandidateTestDetails ) {
 <tr>
    <td>
     @item.TestDate             
    </td>
   <td>
     @item.RegNum
    </td>
</tr>
}
 </table>