使用Entity Framework和NUGET PageList

时间:2017-03-30 21:09:53

标签: asp.net-mvc entity-framework pagedlist

我需要在我的MVC 5应用程序中提供两个不同的过滤器(一个通过文本框控件,另一个通过下拉列表),它使用实体框架。难以使用表中的选定值填充控件。虽然我最初担心多个值(在列表控件中)因为我将具有多个值的表连接到一个具有单个项目的表中,但我还没有超出第1步。我应该指出我使用的是分页列表控件,这可能使事情变得更复杂。

MODEL (相当简单,在这里看不多)

CONTROLLER

使用排序和名称过滤器相当简单

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

            ViewBag.CurrentFilter = searchString;


            var employees = from s in db.tblEmployeeADPs.Include(s => s.tblDepartmentFuncADP)
                            .Where(s => s.Status == "Active")
                            select s;

            if (!String.IsNullOrEmpty(searchString))
            {
                employees = employees.Where(s => s.LastName.Contains(searchString)
                                              || s.FirstName.Contains(searchString));

            }

            if (!String.IsNullOrEmpty(DepartmentFunction))
            {
                employees = employees.Where(s => s.HomeDepartmentDesc == DepartmentFunction);                       

            }

int pageSize = 30;
int pageNumber = (page ?? 1);
return View(employees.ToPagedList(pageNumber, pageSize));

查看

@using (Html.BeginForm("Index","Employee", FormMethod.Get))
{
<p>
   Find by name:  @Html.TextBox("searchString", ViewBag.currentFilter as     String)
    <input type="submit" value="Search" />

   <img style="5px"/> 

   XMan-Filter: @Html.DropDownListFor(m => m.FunctionName, new SelectList(ViewBag.DepartmentFunc, "FunctionName", "FunctionNAme"), "-- Select One --")      
 </p>
}

Page @(Model.PageCount < Model.PageNumber ? 0 : Model.PageNumber) of    @Model.PageCount

@Html.PagedListPager(Model, page => Url.Action("Index",
new { sortOrder = ViewBag.CurrentSort, currentFilter =     ViewBag.CurrentFilter, page, PlaceHolderForVariable}))

当我尝试使用m =&gt; m.valuename lambda语法来测试集合中的任何东西,我收到一个错误:

PagedList.IPagedList'不包含'FunctionName'的定义,并且没有扩展方法'FunctionName'接受'PagedList.IPagedList'类型的第一个参数可以

1 个答案:

答案 0 :(得分:0)

编辑:

@Html.DropDownListFor(m => m.FunctionName, new SelectList(ViewBag.DepartmentFunc, "FunctionName", "FunctionNAme"), "-- Select One --")

将此行编辑为

@Html.DropDownListFor(model => model.FirstOrDefault().FunctionName, new SelectList(ViewBag.DepartmentFunc, "FunctionName", "FunctionName"), "-- Select One --")      

它应该有用。