Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net mvc 如何在mvc中使用下拉列表过滤数据_Asp.net Mvc_Model View Controller - Fatal编程技术网

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

Asp.net mvc 如何在mvc中使用下拉列表过滤数据,asp.net-mvc,model-view-controller,Asp.net Mvc,Model View Controller,我的网站已经有了搜索、排序功能。我还想添加一个过滤功能,但我面临一个问题,即查看页面上的下拉列表不会向控制器返回一个值,该值将过滤掉数据,但不知何故它不会返回,如果可能的话,我不想硬编码下拉列表 管理员控制器 public ViewResult Index(string sortOrder, string currentFilter, string searchString, string currentValue, string sortType, int? page) {

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

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

        }


Index.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 }))

@model PagedList.IPagedList
@使用PagedList.Mvc;
@{
ViewBag.Title=“Index”;
Layout=“~/Views/Shared/_Layout.cshtml”;
}
指数
@使用(Html.BeginForm(“Index”,“Admin”,FormMethod.Get))
{

按名称查找:@Html.TextBox(“SearchString”,ViewBag.CurrentFilter为字符串)
@Html.DropDownList(“MembershipTypesId”,ViewBag.CurrentFilter为字符串)

} 成员资格类型 用户名 名字 @ActionLink(“姓氏”,“索引”,新的{sortOrder=ViewBag.NameSortParm,currentFilter=ViewBag.currentFilter}) 电子邮件 @ActionLink(“出生日期”,“索引”,新的{sortOrder=ViewBag.DateSortParm,currentFilter=ViewBag.currentFilter}) 密码 @foreach(模型中的var项目) { @DisplayFor(modelItem=>item.MembershipTypes.Name) @DisplayFor(modelItem=>item.Username) @DisplayFor(modelItem=>item.FirstName) @DisplayFor(modelItem=>item.LastName) @DisplayFor(modelItem=>item.Email) @DisplayFor(modelItem=>item.DateofBirth) @DisplayFor(modelItem=>item.Password) @ActionLink(“编辑”,“编辑”,新的{id=item.IC})| @ActionLink(“详细信息”,“详细信息”,新的{id=item.IC})| @ActionLink(“删除”,“删除”,新的{id=item.IC}) }
@Model.PageCount的@页(Model.PageCountUrl.Action(“索引”), 新建{page,sortOrder=ViewBag.CurrentSort,currentFilter=ViewBag.currentFilter})
我已经解决了我自己的问题解决方案在AdminController中只需添加另一条需要更改其中新参数的if语句即可

 if (!String.IsNullOrEmpty(searchVar))
            {
                customer = customer.Where(c => c.MembershipTypes.Name.Contains(searchVar));
            }
在此之后,您需要硬编码在dropdownlist的值中,在我的情况下,我没有很多适合硬编码的参考数据

    @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" />
@Html.DropDownList(“SearchVar”),新列表
{
新建SelectListItem{Text=“Assiants”,Value=“Assiants”},
新建SelectListItem{Text=“Secretary”,Value=“Secretary”},
新建SelectListItem{Text=“Ambassador”,Value=“Ambassador”}
},“选择”,ViewBag.CurrentTypes为字符串)