0

PagedListを使用するページがありますが、並べ替えに使用されるドロップダウン リストも含まれています。問題は、誰かがドロップダウンリスト項目をソートに使用するときはいつでも、「次へ」または「戻る」を押すたびに、ドロップダウンリスト項目がデフォルトに復元され、プログラムがデフォルトの (基本的に空白の) ソートでコントローラーを実行することです。 2 ページ目以降 (および戻る) に移動すると、並べ替えられたアイテムが失われます。

このページで使用するコントローラーは次のとおりです。

    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");
}

次に、メモリ内に独自の DropDownlist を作成するなど、ViewModel 内でそれを処理できます。このようなもの。

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 のどこにいても、クリックするたびにドロップダウンから選択した値が常に保持されます。

ビューには、PagedList だけではなく、StuffViewModel を含むモデルが含まれることに注意してください。次のように、ビュー内で要素を呼び出します。

@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 に答える