ASP.NET 将 List 从 Table header ActionLink 中的 IEnumerable @model 传回控制器 non-indexing

ASP.NET passing List back to controller from IEnumerable @model inside Table header ActionLink with non-indexing

我一直在审查 return 一个 IEnumerable 类型的视图的@model 信息返回控制器的可能方法,这样如果我 sort/filter 从数据库查询,我可以改进return 每次迭代都不会重新启动 returned 新的完整列表。所有方法都表明您需要 POST 基于模型 [index] 返回,如果您在 for 循环中,它会起作用。但我正在努力从 table 的 header 部分中的 @HTML.ActionLink 发送回 collection,因此没有可用的索引。

我的 WebAPI 设置基于 this,其中显示了如何排序和过滤。我试图让它变得更复杂一点,因为在我根据原始数据库查询过滤列表后,我将能够排序(从 clickable-actionLink 到 table 的 header 列)来自过滤后的列表;目前它只是从一个新的完整列表中排序。

我能想到的唯一方法是(通过 POST)向控制器传回 customClass 的更新列表。

@Html.ActionLink("Name", "Index", new { orderBy = ViewBag.sortByName, companyListIds = Model.???? })

一个更好的选择(基于 Tacud 的评论)需要更小的 POST URL 是 returning 一个 id 属性的列表,然后可以是应用于查询。但它仍然是一个列表,仍然需要在没有来自 ActionLink 的索引的情况下发回。这将有助于跟踪并允许我继续向下钻取到越来越小的列表。

下面是我的模型的一部分 class、来自控制器的索引 Action 和索引视图。 模型命名空间:

public class Company
{
    public int Id { get; set; }

    [Required]
    public string Name { get; set; }
    public string Address { get; set; }
    public string City { get; set; }
    public string State { get; set; }
}

控制器命名空间:

public async Task<IActionResult> Index(ICollection<int> prev, string orderBy , string searchCategory ="", string searchString = "")
    {
        List<string> Categories = new List<string>() { "Name", "City", "State", "Zip", "Contact Person" };
        ViewBag.searchCategory = new SelectList(Categories);
        ViewBag.sortByName = orderBy == null ? "name" : orderBy == "name" ? "namedesc" : "name";
        ViewBag.sortByCity = orderBy == "city" ? "citydesc" : "city";
        ViewBag.sortByState = orderBy == "state" ? "statedesc" : "state";
        ViewBag.companyIndex = companyList.Count==0 ? await _context.Company.ToListAsync() : companyList ;

        List<Company> resultSet = new List<Company>(ViewBag.companyIndex);

        if (!String.IsNullOrEmpty(searchCategory) && !String.IsNullOrEmpty(searchString))
        {
            switch (searchCategory)
            {
             .....
            }
        }

        switch (orderBy)
        {
          ....
        }

        return View(resultSet);
    }

查看命名空间:

@model IEnumerable<Laier_It.Models.Company> <p>
@using (Html.BeginForm() {
<p>
    Search By: @Html.DropDownList("SearchCategory", "")
    Search For: @Html.TextBox("SearchString")
    <input type="submit" value="Filter" />
</p> }
<table class="table ">
    <thead>
        <tr>
            <th>
                @Html.ActionLink("Name", "Index", new { orderBy = ViewBag.sortByName, companyList = Model })
            </th>
            <th>
                @Html.DisplayNameFor(model => model.Address)
            </th>
            <th>
                @Html.ActionLink("City", "Index", new { orderBy = ViewBag.sortByCity, companyList = Model })
            </th>
            <th>
                @Html.ActionLink("State", "Index", new { orderBy = ViewBag.sortByState, companyList = Model })
            </th> </tr>
    </thead>
    <tbody>
        @foreach (var item in Model)
        {
            <tr>
                <td>
                    @Html.DisplayFor(modelItem => item.Name)
                </td>
                <td>
                    @Html.DisplayFor(modelItem => item.Address)
                </td>
                <td>
                    @Html.DisplayFor(modelItem => item.City)
                </td>
                <td>
                    @Html.DisplayFor(modelItem => item.State)
                </td>  </tr>
        }
    </tbody>
</table>

首先获取我想要 post 返回到 WebAPI 视图页面中显示的当前再现的控制器的 Id 列表,我使用 @Model.Select(x => x.Id)

我的控制器索引方法只被这个

改变了
var resultSet = prev.Count == 0 ? await _context.Company.ToListAsync() : 
               await _context.Company.Where(x => prev.Contains(x.Id)).ToListAsync();

我的视图如下所示:

@model IEnumerable<Laier_It.Models.Company>

@using (Html.BeginForm() )
{
    <p>
        Search By: @Html.DropDownList("SearchCategory", "")
        Search For: @Html.TextBox("SearchString")
        <input type="submit" value="Filter" />
    </p>
}

<table class="table ">
    <thead>
        <tr>
            <th>
                @Html.ActionLink("Name", "Index", new { orderBy = ViewBag.sortByName, prev = @Model.Select(x => x.Id) } )
            </th>
            <th>
                @Html.DisplayNameFor(model => model.Address)
            </th>
            <th>
                @Html.ActionLink("City", "Index", new { orderBy = ViewBag.sortByCity, prev = @Model.Select(x => x.Id) } )
            </th>
            <th>
                @Html.ActionLink("State", "Index", new { orderBy = ViewBag.sortByState, prev = @Model.Select(x => x.Id) } )
            </th>
        </tr>
    </thead>
    <tbody>
        @foreach (var item in Model)
        {
            <tr>
                <td>
                    @Html.DisplayFor(modelItem => item.Name)
                </td>
                <td>
                    @Html.DisplayFor(modelItem => item.Address)
                </td>
                <td>
                    @Html.DisplayFor(modelItem => item.City)
                </td>
                <td>
                    @Html.DisplayFor(modelItem => item.State)
                </td>
            </tr>
        }
    </tbody>
</table>