-1
 <h2>Documenten</h2>
    <table border="1"">
     <tr>   
        <th">
           title
        </th>
        <th>
           description
        </th>
         <th>
             Download
         </th>
    </tr>*  



  @foreach (var item in Model.Trajects.Components)
    {

        if (item.getType() == "document")
        {

        <tr>
                 <td>

                     @item.Title
                 </td>
                 <td>
                     @item.Description
                 </td>
                 <td><a href='@Url.Action("Download","Component", new               {componentid=item.componentID,l=Request["trajectcode"],g = Model})'>
                        <img src='@Url.Content("../../Images/play.png")' alt="Home Page">
                    </a> </td>
        </tr>            
        } 

    }
    </table>

现在,当我初始化视图时,按标题对这个表进行排序的最佳方法是什么?

4

1 回答 1

0

我更喜欢在控制器中对其进行排序,但您可以执行以下操作:

@foreach (var item in Model.Trajects.Components.OrderBy(c => c.Title)) 
{
    if (item.getType() == "document")
    {
        <tr>
            <td>@item.Title</td>
            <td>@item.Description</td>
            <td>
                <a href='@Url.Action("Download","Component", new { componentid = item.componentID, l = Request["trajectcode"], g = Model })'>
                    <img src='@Url.Content("../../Images/play.png")' alt="Home Page">
                </a>
            </td>
        </tr>            
    } 
}

请参阅OrderByforeach 语句。

于 2013-05-18T21:37:04.840 回答