0

I need to provide two distinct filters in my MVC 5 application (one via text box control and the other via drop down list), which uses the Entity Framework. Having difficulty populating the control with selected values from the table. While I was initially worried about multiple values (in the list control) because I joined a table with multiple values into one with single items, I haven’t gotten beyond step 1. I should point out that I am using a paged list control, which may be complicating things more.

MODEL (Fairly straightforward with not much to see here)

CONTROLLER

Fairly straightforward with a sort and name filter

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

VIEW

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

When I attempt to use the m => m.valuename lambda syntax to test anything out of the collection, I get an error:

PagedList.IPagedList' does not contain a definition for 'FunctionName' and no extension method 'FunctionName' accepting a first argument of type 'PagedList.IPagedList' could

  • The last argument in the SelectList is `FunctionNAme`. Is this correct? – Hamza Anis Mar 30 '17 at 21:21
  • Yes, slight typo there as it should have been FunctionName – Thomas Washington Mar 30 '17 at 21:24
  • Because you obviously have `@model IPagedList` declared in your model and that dos not contain a property named `FunctionName` Create a view model that contains a property (say) `string FunctionName` to bind the dropdownlist to and a property `IPagedList Employees` (and then you can get rid of all the other horrible `ViewBag` properties as well and strongly bind to you model) –  Mar 30 '17 at 23:59
  • Refer [this answer](http://stackoverflow.com/questions/42450257/how-to-i-apply-filter-while-paginating-in-asp-net-mvc-and-entity-framework/42450564#42450564) for an example –  Mar 31 '17 at 00:02

1 Answers1

0

EDIT:

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

Edit this line to

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

It should work.

Hamza Anis
  • 2,475
  • 1
  • 26
  • 36
  • I'm going to triple check this again, but it's throwing the following error after that adjustment: "Templates can be used only with field access, property access, single-dimension array index, or single-parameter custom indexer expressions. " – Thomas Washington Mar 30 '17 at 21:30
  • Import `using System.ComponentModel.DataAnnotations;` in your model. – Hamza Anis Mar 30 '17 at 21:32
  • I added this reference to both tables in the model (Employee and Department-Function). It was previously included in my Metadata class so I can use more readable names. However, it's still throwing the same error after I rebuild the project. – Thomas Washington Mar 30 '17 at 21:41
  • @ThomasWashington http://stackoverflow.com/a/24488765/4544967 See this – Hamza Anis Mar 30 '17 at 21:48