如何在mvc中使用下拉列表过滤数据



我的网站已经有了搜索、排序功能。我也想添加一个过滤功能,但遇到了一个问题,即视图页面上的下拉列表不会向将过滤掉数据的控制器返回值,但不知何故,它不会,如果可能的话,我不想硬编码下拉列表
管理员控制器

public ViewResult Index(string sortOrder, string currentFilter, string searchString, string currentValue, string sortType, int? page)
{
ViewBag.CurrentSort = sortOrder;
ViewBag.sortType = new SelectList(db.MembershipTypes, "MembershipTypesId", "Name");
ViewBag.CurrentType = sortType;
//sorting
ViewBag.NameSortParm = String.IsNullOrEmpty(sortOrder) ? "name_desc" : ""; 
ViewBag.DateSortParm = sortOrder == "Date" ? "date_desc" : "Date";
if (searchString != null || sortType != null)
{page = 1;}
else
{
searchString = currentFilter;
sortType = currentFilter;
}
ViewBag.CurrentFilter = searchString;

var customer = from a in db.Customers.Include(c => c.MembershipTypes) select a;
var x = from b in db.MembershipTypes select b;
//search
if (!String.IsNullOrEmpty(searchString))
{
customer = customer.Where(c => c.LastName.Contains(searchString)
|| c.FirstName.Contains(searchString));
}
//sorting
switch (sortOrder)
{
case "name_desc":
customer = customer.OrderByDescending(c => c.LastName);
break;
case "Date":
customer = customer.OrderBy(c => c.DateofBirth);
break;
case "date_desc":
customer = customer.OrderByDescending(c => c.DateofBirth);
break;
default:
customer = customer.OrderBy(c => c.LastName);
break;
}
int pageSize = 3;
int pageNumber = (page ?? 1);
return View(customer.ToPagedList(pageNumber, pageSize));
}

索引.cshtml

@model PagedList.IPagedList<LeafLife.Models.Customers>
@using PagedList.Mvc;
<link href="~/Content/PagedList.css" rel="stylesheet" type="text/css" />
@{
ViewBag.Title = "Index";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<h2>Index</h2>
@using (Html.BeginForm("Index", "Admin", FormMethod.Get))
{
<p>
Find by name: @Html.TextBox("SearchString", ViewBag.CurrentFilter as string)
@Html.DropDownList("MembershipTypesId", ViewBag.CurrentFilter as string)
<input type="submit" value="Search" />
</p>
}
<table class="table">
<tr>
<th>
MembershipTypes
</th>
<th>
Username
</th>
<th>
FirstName
</th>
<th>
@Html.ActionLink("Last Name", "Index", new { sortOrder = ViewBag.NameSortParm, currentFilter = ViewBag.CurrentFilter })
</th>
<th>
Email
</th>
<th>
@Html.ActionLink("Date of Birth", "Index", new { sortOrder = ViewBag.DateSortParm, currentFilter = ViewBag.CurrentFilter })
</th>
<th>
Password
</th>
<th></th>
</tr>
@foreach (var item in Model)
{
<tr>
<td>
@Html.DisplayFor(modelItem => item.MembershipTypes.Name)
</td>
<td>
@Html.DisplayFor(modelItem => item.Username)
</td>
<td>
@Html.DisplayFor(modelItem => item.FirstName)
</td>
<td>
@Html.DisplayFor(modelItem => item.LastName)
</td>
<td>
@Html.DisplayFor(modelItem => item.Email)
</td>
<td>
@Html.DisplayFor(modelItem => item.DateofBirth)
</td>
<td>
@Html.DisplayFor(modelItem => item.Password)
</td>
<td>
@Html.ActionLink("Edit", "Edit", new { id = item.IC }) |
@Html.ActionLink("Details", "Details", new { id = item.IC }) |
@Html.ActionLink("Delete", "Delete", new { id = item.IC })
</td>
</tr>
}
</table>
<br />
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 }))

我已经解决了我自己的问题,解决方案在AdminController中,只需添加另一个if语句,该语句需要更改中的新参数

if (!String.IsNullOrEmpty(searchVar))
{
customer = customer.Where(c => c.MembershipTypes.Name.Contains(searchVar));
}

在那之后,你需要在下拉列表的值中进行硬编码,在我的情况下,我没有很多适合在中硬编码的参考数据

@Html.DropDownList("SearchVar", new List<SelectListItem>
{
new SelectListItem{ Text="Assiants", Value = "Assiants" },
new SelectListItem{ Text="Secretary", Value = "Secretary" },
new SelectListItem{ Text="Ambassador", Value = "Ambassador" }
}, "Select", ViewBag.CurrentTypes as string)
<input type="submit" value="Search" />

最新更新