Asp.net mvc 3 我的视图需要一个html助手,但不太确定使用哪一个

Asp.net mvc 3 我的视图需要一个html助手,但不太确定使用哪一个,asp.net-mvc-3,html,view,html-helper,Asp.net Mvc 3,Html,View,Html Helper,我正在制作一个MVC3 web应用程序。我有一个表,其中充满了我数据库中的数据,但我想知道如何才能使它只显示概要的前三行,而不是全部10行。我认为这与HTML帮助程序有关,但我想知道你们是否可以帮助我找到正确的代码????? 任何帮助都将是巨大的 @model IEnumerable<TheatreGroup.Models.Show> @{ ViewBag.Title = "Shows"; } <h2> Shows</h2> <

我正在制作一个MVC3 web应用程序。我有一个表,其中充满了我数据库中的数据,但我想知道如何才能使它只显示概要的前三行,而不是全部10行。我认为这与HTML帮助程序有关,但我想知道你们是否可以帮助我找到正确的代码????? 任何帮助都将是巨大的

    @model IEnumerable<TheatreGroup.Models.Show>
@{
    ViewBag.Title = "Shows";
}
<h2>
    Shows</h2>

<p>Below is a list of shows which will be avalible in the next couple of weeks</p>

<p>
    @Html.ActionLink("Create New", "Create")
</p>
@using (Html.BeginForm())
{ 
    <p>
        Find by name: @Html.TextBox("SearchString")
        <input type="submit" value="Search" /></p>
    }
<table>
    <tr>
        <th>
            @Html.ActionLink("name", "Index", new { sortOrder = ViewBag.nameSortParm,})
        </th>
        <th>
            @Html.ActionLink("writer", "Index", new { sortOrder = ViewBag.writerSortParm,})
        </th>
        <th>
            @Html.ActionLink("synopsis", "Index", new { sortOrder = ViewBag.synopsisSortParm,  })
        </th>
        <th>
        </th>
    </tr>
    @foreach (var item in Model){

        <tr>
            <td>
                @Html.DisplayFor(modelItem => item.name)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.writer)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.synopsis)
            </td>
            <td>
                @Html.ActionLink("Edit", "Edit", new { id = item.showid }) |
                @Html.ActionLink("Details", "Details", new { id = item.showid }) |
                @Html.ActionLink("Delete", "Delete", new { id = item.showid })
            </td>
        </tr>
    }
</table>
@model IEnumerable
@{
ViewBag.Title=“显示”;
}
显示
以下是几周内将推出的节目列表

@ActionLink(“新建”、“创建”)

@使用(Html.BeginForm()) { 按名称查找:@Html.TextBox(“搜索字符串”)

} @ActionLink(“name”,“Index”,new{sortOrder=ViewBag.nameSortParm,}) @ActionLink(“writer”,“Index”,new{sortOrder=ViewBag.writerSortParm,}) @ActionLink(“概要”,“索引”,新的{sortOrder=ViewBag.synopsisSortParm,}) @foreach(模型中的var项目){ @DisplayFor(modelItem=>item.name) @DisplayFor(modelItem=>item.writer) @DisplayFor(modelItem=>item.Synosis) @ActionLink(“编辑”,“编辑”,新的{id=item.showid})| @ActionLink(“详细信息”,“详细信息”,新的{id=item.showid})| @ActionLink(“删除”,“删除”,新的{id=item.showid}) }
您正在使用
@foreach(模型中的var项)
显示每一行


尝试使用
@for(int i=0;i m[i].name)
等来显示的。我相信这应该行。

您正在使用
@foreach(模型中的var项)
来显示每一行


尝试使用
@for(int i=0;i m[i].name)
等来显示的。我相信这应该行得通。

我将我的答案分为两种状态:

  • 你需要其他7行在页面流程的下游(如“显示更多结果”链接)

  • 你不需要列表中的其他部分

  • 解决方案1:

    用“危险”说的话

    @for (int i=0; i<3; i++)
    {
        @Html.DisplayFor(m => m[i].name) // And the rest of the data
    }
    
    您可能需要一个ToList()使其工作,这取决于

    这样,您就可以保持代码不变,只需缩短发送到视图的元素数量


    希望有帮助

    我将我的答案分为两种状态:

  • 你需要其他7行在页面流程的下游(如“显示更多结果”链接)

  • 你不需要列表中的其他部分

  • 解决方案1:

    用“危险”说的话

    @for (int i=0; i<3; i++)
    {
        @Html.DisplayFor(m => m[i].name) // And the rest of the data
    }
    
    您可能需要一个ToList()使其工作,这取决于

    这样,您就可以保持代码不变,只需缩短发送到视图的元素数量


    希望有帮助

    +1致Omer,以提醒其注意视图模型应仅包含要在视图中显示的信息。其中解决方案2更好,只填充控制器中需要的内容。然后,你可以使用你的原始为每个循环,这是清洁。我的解决方案只是一个快速修复+1致Omer,以提醒其注意视图模型应仅包含要在视图中显示的信息。其中解决方案2更好,只填充控制器中需要的内容。然后,你可以使用你的原始为每个循环,这是清洁。我的解决方案只是一个快速修复!