如何在MVC c#中存储以前的搜索数据和搜索类别?

时间:2014-08-16 12:34:45

标签: c# asp.net-mvc

我将尝试保存以前的搜索结果和数据。例如,如果我搜索单词' apple',则结果显示按apple排序。我实现了部分排序,这意味着当我点击时按特定类别排序。那时,搜索功能被初始化。此外,当我再次搜索时,结果显示原始上下文。 我需要做些什么来实现这些?

以下是控制器的代码。

public ActionResult Index(string searchField, string sortOrder, string currentFilter, string searchString, int? page, string commandName)
    {
        ViewBag.CurrentSort = sortOrder;
        ViewBag.DateSortParm = sortOrder == "Date" ? "date_desc" : "Date";
        ViewBag.PostNumSortParm = sortOrder == "PostNum" ? "post_desc" : "PostNum";
        ViewBag.HitsSortParm = sortOrder == "HitsNum" ? "hits_desc" : "HitsNum";

        if (searchString != null)
        {
            page = 1;
        }
        else
        {
            searchString = currentFilter;
        }

        ViewBag.CurrentFilter = searchString;

        var bulletins = from s in db.Bulletins.ToList().OrderByDescending(b => b.postNum)
                        select s;

        switch (sortOrder)
        {
            case "PostNum":
                bulletins = bulletins.OrderBy(s => s.postNum);
                break;
            case "post_desc":
                bulletins = bulletins.OrderByDescending(s => s.postNum);
                break;
            case "HitsNum":
                bulletins = bulletins.OrderBy(s => s.readCnt);
                break;
            case "hits_desc":
                bulletins = bulletins.OrderByDescending(s => s.readCnt);
                break;
            case "Date":
                bulletins = bulletins.OrderBy(s => s.PostDate);
                break;
            case "date_desc":
                bulletins = bulletins.OrderByDescending(s => s.PostDate);
                break;
            default:
                break;
        }

        ViewBag.searchField = new[] { new SelectListItem() { Text = "All", Value = "All"},
                                      new SelectListItem() { Text = "Name", Value = "Name"},
                                      new SelectListItem() { Text = "Title", Value = "Title"}};

        switch (searchField)
        {
            case "Name":
                if (!String.IsNullOrEmpty(searchString))
                {
                    bulletins = bulletins.Where(s => s.Name.Contains(searchString));
                }
                break;

            case "Title":
                if (!String.IsNullOrEmpty(searchString))
                {
                    bulletins = bulletins.Where(s => s.Title.Contains(searchString));
                }
                break;

            default:
                if (!String.IsNullOrEmpty(searchString))
                {
                    bulletins = bulletins.Where(s => s.Title.Contains(searchString)
                        || s.Name.Contains(searchString));
                }
                break;
        }

        if (commandName == "NEW POST")
            return RedirectToAction("Create");
        if (commandName == "DELETE POST")
            return RedirectToAction("DeleteSelected");

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

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

我创建了SelectListItem以将搜索类别保留在下拉列表中。

这是索引视图。

<table class="table">
@using (Html.BeginForm("Index", "Bulletin", FormMethod.Get, new { searchField = ViewBag.searchField }))
{
    <p>
        <div class="editor-field">
            @Html.DropDownList("searchField")
            @Html.TextBox("SearchString", ViewBag.searchString as string)
            <input type="submit" value="Search" />
        </div>
    </p>
}

<tr>
    <td style="width: 20%">
        Select
    </td>
    <td style="width: 20%">
        @Html.ActionLink("Post No.", "Index", new { sortOrder = ViewBag.PostNumSortParm, currentFilter = ViewBag.CurrentFilter, searchField = ViewBag.searchField })
    </td>
    <td style="width: 30%">
        Title
    </td>
    <td style="width: 30%">
        Name
    </td>
    <td style="width: 30%">
        @Html.ActionLink("Post Date", "Index", new { sortOrder = ViewBag.DateSortParm, currentFilter = ViewBag.CurrentFilter, searchField = ViewBag.searchField })
    </td>
    <td style="width: 30%">
        @Html.ActionLink("Hits", "Index", new { sortOrder = ViewBag.HitsSortParm, currentFilter = ViewBag.CurrentFilter, searchField = ViewBag.searchField })
    </td>
    <td style="width: 40%">
        Response
    </td>
    <th></th>
</tr>


@using (Html.BeginForm("DeleteSelected", "Bulletin", FormMethod.Post, htmlAttributes: new { id = "DeleteID", searchField = ViewBag.searchField }))
{
    foreach (var item in Model)
    {
        <tr>

            <td>
                <input type="checkbox" name="ids" value="@item.postNum" />
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.postNum)
            </td>
            <td>
                <a href="/Bulletin/Details/@item.postNum">@item.Title</a>
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.Name)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.PostDate)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.readCnt)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.reviewCnt)
            </td>
        </tr>
    }
}
</table>

<br />
<div style="text-align:center">
@Html.PagedListPager(Model, page => Url.Action("Index",
new { page, sortOrder = ViewBag.CurrentSort, currentFilter = ViewBag.CurrentFilter, searchField = ViewBag.searchField }))

我跳过了一些似乎不重要的代码。我很难赶上我失踪的东西。

你可以给我帮忙吗?

0 个答案:

没有答案