MVC C#存储搜索类别

时间:2014-08-15 17:05:45

标签: c# asp.net-mvc

我尝试实现搜索功能。它有效,但我需要制作混凝土。

某些功能无法正常工作。

如果我点击搜索按钮,即使我选择了另一个选项,下拉列表也会更改为全部。

另外,我想尝试进行深度搜索,例如按特定搜索项目进行排序。

这是控制器索引。

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

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

        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 = 10;
        int pageNumber = (page ?? 1);

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

这是搜索的索引部分..

@model PagedList.IPagedList<BulletinBoard.Models.Bulletin>
@using PagedList.Mvc;
<link href="~/Content/PagedList.css" rel="stylesheet" type="text/css" />

@{
    ViewBag.Title = "New Post";
}

 <br />
<h2>New Post</h2>

@using (Html.BeginForm("Index", "Bulletin", FormMethod.Get))
{
    <p>
        <select id="searchField" name="searchField">
        <option value="All">All</option>
        <option value="Name">Name</option>
        <option value="Title">Title</option>
    </select>
    @*@Html.TextBox("SearchString", ViewBag.CurrentFilter as string)*@
    <input type="text" name="searchString" value="" />
    <input type="submit" value="Search" />
</p>
}

<table class="table">
<tr>
    <td style="width: 20%">
        Select
    </td>
    <td style="width: 20%">
        @Html.ActionLink("Post No.", "Index", new { sortOrder = ViewBag.PostNumSortParm, currentFilter = ViewBag.CurrentFilter })
    </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 })
    </td>
    <td style="width: 30%">
        @Html.ActionLink("Hits", "Index", new { sortOrder = ViewBag.HitsSortParm, currentFilter = ViewBag.CurrentFilter })
    </td>
    <td style="width: 40%">
        Response
    </td>
    <th></th>
</tr>


@using (Html.BeginForm("DeleteSelected", "Bulletin", FormMethod.Post, htmlAttributes: new {id ="DeleteID"}))
{
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>
}
<tr>
    <td>

        <div align="right">

            <input type="submit" name="commandName" value="DELETE POST" />
        </div>

    </td>
</tr>
}

@using (Html.BeginForm("Index", "Bulletin", FormMethod.Get))
{
<tr>
    <td>
        <div align="right">
            <input type="submit" name="commandName" value="NEW POST" />
        </div>
    </td>
</tr>
}

@Scripts.Render("~/bundles/jqueryval")



<script type="text/javascript">
    $(document).ready(function () {
        $("DeleteID").click(function () {
            $("DeleteSelected").submit();
        });
    });
</script>

<br />
    <div style="text-align:center">
Page @(Model.PageCount < Model.PageNumber ? 0 : Model.PageNumber) of @Model.PageCount

@Html.PagedListPager(Model, page => Url.Action("Index",
new { page, sortOrder = ViewBag.CurrentSort, currentFilter = ViewBag.CurrentFilter, searchField = Request["searchField"], searchString = Request["searchString"] }))

我使用了带有排序类别的selectListItem,并在索引视图中使用了dropdownlist。 所以,我解决了一下,但在排序之后,在对信息和度假村进行排序后,我需要做些什么才能在搜索框中保留数据?

例如,关键字是banana。按香蕉排序。我按不同类别排序。并且,香蕉仍然存在。

1 个答案:

答案 0 :(得分:1)

您的选择框未在任何位置设置所选项目。如果你想维护它,你需要有一些逻辑来跨帖子这样做。根据您当前的模板,最简单的方法可能是在viewbag中为您设置selected属性设置一个值。