0

我有一个使用PagedList的页面,但也包含一个用于排序的下拉列表。问题是,每当有人使用下拉列表项进行排序时,每当他们点击“下一个”或“后退”时,下拉列表项都会恢复为默认值,程序通过控制器运行默认(基本上是空白)排序标准,当您导航到第二页或第二页(并返回)时,您会丢失已排序的项目。

这是用于此页面的控制器:

    public ActionResult Index(FormCollection dropDownSelection, string currentFilter, int? page)
    {
        //security
        if (!Security.IsViewer(User)) return RedirectToAction("Message", "Home", new { id = 2 });

        if (ViewBag.Level == 0) return RedirectToAction("Message", "Home", new { id = 2 });

        if (!(Request.HttpMethod == "GET"))
        {
            page = 1;
        }


        string table = String.IsNullOrWhiteSpace(dropDownSelection["Table"]) ? "%" : dropDownSelection["Table"];
        string issue = String.IsNullOrWhiteSpace(dropDownSelection["IssueType"]) ? "%" : dropDownSelection["IssueType"];
        string status = String.IsNullOrWhiteSpace(dropDownSelection["Status"]) ? "%" : dropDownSelection["Status"];

        var followUpItem = from follow in db.FollowUpItems
                           where (follow.TableName.Equals(table) || table.Equals("%")) &&
                                 (follow.IssueType.Equals(issue) || issue.Equals("%")) &&
                                 (follow.Status.Equals(status) || status.Equals("%"))
                           orderby follow.Id
                           select follow;

        int pageNumber = (page ?? 1);
        int pageSize = 10;

        return View(followUpItem.ToPagedList(pageNumber, pageSize));
    }

这是视图中的下拉列表:

          <select name="Table" title="Table" style="font-size:8pt;">
            <option value="%">--Table Name--</option>
            <option value="AgentContEd">CE</option>
            <option value="AgentProductTraining">PT</option>
          </select>
          <select name="IssueType" style="font-size:8pt;">
            <option value="%">--Issue Type--</option>
            <option value="W">Warning</option>
            <option value="E">Error</option>
          </select>
          <select name="Status" style="font-size:8pt;">
            <option value="%">--Status Type--</option>
            <option value="O">Open</option>
            <option value="U">Under Review</option>
          </select>

并且(以防万一)下面是<div>包含视图中的 PagedList 导航按钮的:

<div>
Page @(Model.PageCount < Model.PageNumber ? 0 : Model.PageNumber)
of @Model.PageCount

@if (Model.HasPreviousPage)
{
    @Html.ActionLink("<<", "Index", new { page = 1, sortOrder = ViewBag.CurrentSort, searchString = ViewBag.CurrentFilter, currentFilter = ViewBag.CurrentFilter })
    @Html.Raw(" ");
    @Html.ActionLink("< Prev", "Index", new { page = Model.PageNumber - 1, sortOrder = ViewBag.CurrentSort, searchString = ViewBag.CurrentFilter, currentFilter = ViewBag.CurrentFilter })
}
else
{
    @:<<
    @Html.Raw(" ");
    @:< Prev
}

@if (Model.HasNextPage)
{
    @Html.ActionLink("Next >", "Index", new { page = Model.PageNumber + 1, sortOrder = ViewBag.CurrentSort, searchString = ViewBag.CurrentFilter, currentFilter = ViewBag.CurrentFilter })
    @Html.Raw(" ");
    @Html.ActionLink(">>", "Index", new { page = Model.PageCount, sortOrder = ViewBag.CurrentSort, searchString = ViewBag.CurrentFilter, currentFilter = ViewBag.CurrentFilter })
}
else
{
    @:Next >
    @Html.Raw(" ")
    @:>>
}
</div>
4

2 回答 2

0

如果您首先通过 ViewModel 传递 followUpItem,然后实际传递下拉列表中的选定值,该怎么办?也许接近这个。

public ActionResult Index(FormCollection dropDownSelection, string currentFilter, int? page)
{
    //Stuff abbreviated 

    return View(new StuffViewModel(followUpItem.ToPagedList(pageNumber, pageSize)), "SelectedDropDownItem");
}

然后你可以在 ViewModel 中处理它,例如在内存中创建你自己的 DropDownlist。像这样的东西。

public class StuffViewModel
{
    public List<FollowUpItem> FollowUpItems {get; private set;}
    public List<SelectListItem> DropDownList {get; private set}

    public StuffViewModel(List<FollowUpItem> followUpItems, string selectedDropDownValue)
    {
        FollowUpItems = followUpItems;

        List<SelectListItem> selectList = new List<SelectListItem>();
        //TODO: here you would decide which is selected from the selectedDropDownValue parameter
        selectList.Add(new SelectListItem{ Selected = true, Text = "Text", Value = "Value" });
        selectList.Add(new SelectListItem{ Selected = false, Text = "Text2", Value = "Value2" });
        //...and more values as you need them

        DropDownList = selectList
    }
}

通过使用 ViewModel 执行此操作,无论您在 pagedList 中的哪个位置,每次单击都将始终保留从下拉列表中选择的值。

请记住,您的视图现在将包含一个包含 StuffViewModel 而不是仅包含 PagedList 的模型。现在你在视图中调用你的元素,如下所示:

@model StuffViewModel

//TODO: access dropdownlist
@Html.DropDownList("name", Model.DropDownList)

//TODO: access PagedList for instenace like this:
if(Model.FollowUpItems.HasPreviousPage)
{
    //do stuff
}

我希望我有任何意义:)

于 2013-02-26T18:16:13.467 回答
0

这很简单。您没有保留下拉选择。分页链接需要包含当前的下拉选择,以便当您向下一页提交 GET 请求时,数据会在查询字符串中传递以用于呈现视图。

于 2013-02-26T18:30:33.140 回答