【问题标题】:Populating DropDownList of MVC Application Using Entity Framework and NUGET PageList使用实体框架和 NUGET PageList 填充 MVC 应用程序的 DropDownList
【发布时间】:2017-03-30 21:09:53
【问题描述】:

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

型号 (相当简单,这里没什么可看的)

控制器

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

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 => m.valuename lambda 语法来测试集合中的任何内容时,我收到一个错误:

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

【问题讨论】:

  • SelectList 中的最后一个参数是FunctionNAme。这是正确的吗?
  • 是的,这里有轻微的错字,应该是 FunctionName
  • 因为您显然在模型中声明了 @model IPagedList&lt;T&gt; 并且不包含名为 FunctionName 的属性创建一个包含属性(例如)string FunctionName 的视图模型以将下拉列表绑定到和一个属性IPagedList&lt;T&gt; Employees(然后你可以摆脱所有其他可怕的ViewBag 属性并强烈绑定到你的模型)
  • this answer为例

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


【解决方案1】:

编辑:

@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 --")      

它应该可以工作。

【讨论】:

  • 我要再次检查三次,但在调整后会抛出以下错误:“模板只能用于字段访问、属性访问、单维数组索引或单参数自定义索引器表达式。"
  • 在您的模型中导入using System.ComponentModel.DataAnnotations;
  • 我将此引用添加到模型中的两个表(Employee 和 Department-Function)。它以前包含在我的 Metadata 类中,因此我可以使用更具可读性的名称。但是,在我重建项目后它仍然抛出同样的错误。
  • @ThomasWashington stackoverflow.com/a/24488765/4544967 看到这个
猜你喜欢
  • 2015-02-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-10-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多