Solved

MVC Page List on Grid ... does not contain a definition for

Posted on 2014-10-17
5
1,549 Views
Last Modified: 2014-10-20
I have the following code and I'm getting this error.

Compiler Error Message: CS1061: 'PagedList.IPagedList<MyAppPhoneListApplication.Models.MasterList>' does not contain a definition for 'FirstName' and no extension method 'FirstName' accepting a first argument of type 'PagedList.IPagedList<MyAppPhoneListApplication.Models.MasterList>' could be found (are you missing a using directive or an assembly reference?)



So does this mean this is not declared correctly?

   <th>
            @Html.DisplayNameFor(model => model.FirstName)
        </th>


I decided to use the page list PagedList.IPagedList instead of the *@model IEnumerable

@*@model IEnumerable<MyAppPhoneListApplication.Models.MasterList>*@

@model PagedList.IPagedList<MyAppPhoneListApplication.Models.MasterList>
@using PagedList.Mvc;

<link href="~/Content/PagedList.css" rel="stylesheet" type="text/css" />

 



@{
    ViewBag.Title = "Tweddle Phone List Master";
}

<h2>Tweddle Phone List Master</h2>

<p>
    @Html.ActionLink("Create New", "Create")
</p>

@*@using (Html.BeginForm()) { 
    <p>

        Find by Last Name or First Name: @Html.TextBox("SearchString")
        Location: @Html.DropDownList("LocationList", "All")
        <input type="submit" value="Search" />
    </p>

 }*@


@using (Html.BeginForm("Index","Tweddle Phone List Master",FormMethod.Get))
{
    <p>

        Find by Last Name or First Name: @Html.TextBox("SearchString", ViewBag.CurrentFilter as String)
        Location: @Html.DropDownList("LocationList", "All")
        <input type="submit" value="Search" />
    </p>

}




<table class="table">
    <tr>
        @*<th>
            @Html.DisplayNameFor(model => model.LastName)
        </th>*@


        <th>
            @Html.ActionLink("Last Name", "Index", new { sortOrder = ViewBag.NameSort })
        </th>
  
        <th>
            @Html.DisplayNameFor(model => model.FirstName)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.Department)
        </th>
        
        
        
        @*<th>
            @Html.DisplayNameFor(model => model.Location)
        </th>*@
        
        
        <th>
            @Html.ActionLink("Location", "Index", new { sortOrder = ViewBag.LocationSort })
        </th>
  
        <th>
            @Html.DisplayNameFor(model => model.ExtensionInternal)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.ExtensionDirectDial)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.NumberMobile)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.NumberHome)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.NumberTextMessage)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.NumberOther)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.Terminated)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.Shift)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.Notes)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.NameGrouping)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.LastUpdatedDT)
        </th>
        <th></th>
    </tr>

@foreach (var item in Model) {
    <tr>
        <td>
            @Html.DisplayFor(modelItem => item.LastName)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.FirstName)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Department)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Location)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.ExtensionInternal)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.ExtensionDirectDial)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.NumberMobile)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.NumberHome)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.NumberTextMessage)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.NumberOther)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Terminated)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Shift)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Notes)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.NameGrouping)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.LastUpdatedDT)
        </td>
        <td>
            @Html.ActionLink("Edit", "Edit", new { id=item.RecordID }) |
            @Html.ActionLink("Details", "Details", new { id=item.RecordID }) |
            @Html.ActionLink("Delete", "Delete", new { id=item.RecordID })
        </td>
    </tr>
}

</table>
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 }) )

Open in new window



My Controller:

public ActionResult Index(String sortOrder, string currentFilter, String LocationList, String SearchString, int? page )
        {


            //var pageIndex = (page ?? 1) - 1;
            //var pageSize = 10;
            //int totalUserCount;
                        
            //Added the page watch 
            ViewBag.CurrentSort = sortOrder;

            ViewBag.NameSort = String.IsNullOrEmpty(sortOrder) ? "Name_desc" : "";
            ViewBag.LocationSort = sortOrder == "Location" ? "Location_desc" : "Location";

            if (SearchString != null)
            {

             page = 1;
                
            }
            else {
                                
                SearchString = currentFilter;                 
             }
            ViewBag.CurrentFilter = SearchString;
            
                       
            var PhoneResults = from r in db.MasterLists
                               select r;
                     
         
            ViewBag.CurrentFilter = SearchString;

                switch (sortOrder){
               
                case "Location":
                    PhoneResults = PhoneResults.OrderBy(s => s.Location);
                    break;
                case "Location_desc":
                    PhoneResults = PhoneResults.OrderByDescending(s => s.Location);
                    break;
                case "Name_desc":
                    PhoneResults = PhoneResults.OrderByDescending(s => s.LastName);
                    break;
                default:
                    PhoneResults = PhoneResults.OrderBy(s => s.LastName);
                    break;
                }

                   
            var LocationListd = new List<String>();
            
            var LocationListQuerry = from r in db.MasterLists
                                     orderby r.Location
                                     select r.Location;

            LocationListd.AddRange(LocationListQuerry.Distinct());
                        ViewBag.LocationList = new SelectList(LocationListd);       
           
            if (!String.IsNullOrEmpty(SearchString)) {
                PhoneResults = PhoneResults.Where(s => s.LastName.ToUpper().Contains(SearchString.ToUpper()) ||
                     s.FirstName.ToUpper().Contains(SearchString.ToUpper()));
                            }

            if (!String.IsNullOrEmpty(LocationList)) { 
               PhoneResults = PhoneResults.Where(s => s.Location == LocationList);
                }

             //return View(db.MasterLists.ToList());
           //return View(PhoneResults.ToList());

           int pageSize = 3;
           int pageNumber = (page ?? 1);
           //
            return View(PhoneResults.ToPagedList(pageNumber, pageSize));
         
        
      }

Open in new window

0
Comment
Question by:yguyon28
  • 3
  • 2
5 Comments
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 40386695
Your model is a collection of things, but you are trying to dump out the properties of the individual things in the collection. You need to loop over the model--since it is a collection--and then dump out the properties. You're actually already doing that (it would seem). Try commenting out lines 48 - 110. Everything that remains should be fine.
0
 

Author Comment

by:yguyon28
ID: 40386820
Why should I comment line 48-110, this will remove my header row... Does not work
0
 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 500 total points
ID: 40386885
I didn't look close enough at what that code was doing. It looked like a copy of the stuff below.

I'm not terribly fond of this approach, but you might try including a call to First in each header item:

e.g.

<table class="table">
    <tr>
        @*<th>
            @Html.DisplayNameFor(model => model.First().LastName)
        </th>*@


        <th>
            @Html.ActionLink("Last Name", "Index", new { sortOrder = ViewBag.NameSort })
        </th>
  
        <th>
            @Html.DisplayNameFor(model => model.First().FirstName)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.First().Department)
        </th>
        
        
        
        @*<th>
            @Html.DisplayNameFor(model => model.First().Location)
        </th>*@
        
        
        <th>
            @Html.ActionLink("Location", "Index", new { sortOrder = ViewBag.LocationSort })
        </th>
  
        <th>
            @Html.DisplayNameFor(model => model.First().ExtensionInternal)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.First().ExtensionDirectDial)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.First().NumberMobile)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.First().NumberHome)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.First().NumberTextMessage)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.First().NumberOther)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.First().Terminated)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.First().Shift)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.First().Notes)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.First().NameGrouping)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.First().LastUpdatedDT)
        </th>
        <th></th>
    </tr>

Open in new window


This will fail if there are no items in the collection, so you'll want to code defensively around that.
0
 

Author Comment

by:yguyon28
ID: 40387242
It does work now... But my drop down and search box doesn't work any more after implementing this solution? Yes I get a grid with all my result but they are no longer displayed in sort order by location and I can't do a search on the Name or by location on the drop down.


I get this type of error

Server Error in '/' Application.

The resource cannot be found.


View:

@*@model IEnumerable<MyAppPhoneListApplication.Models.MasterList>*@

@model PagedList.IPagedList<MyAppPhoneListApplication.Models.MasterList>
@using PagedList.Mvc;

<link href="~/Content/PagedList.css" rel="stylesheet" type="text/css" />

 



@{
    ViewBag.Title = "Tweddle Phone List Master";
}

<h2>Tweddle Phone List Master</h2>

<p>
    @Html.ActionLink("Create New", "Create")
</p>

@*@using (Html.BeginForm()) { 
    <p>

        Find by Last Name or First Name: @Html.TextBox("SearchString")
        Location: @Html.DropDownList("LocationList", "All")
        <input type="submit" value="Search" />
    </p>

 }*@


@using (Html.BeginForm("Index","Tweddle Phone List Master",FormMethod.Get))
{
    <p>

        Find by Last Name or First Name: @Html.TextBox("SearchString", ViewBag.CurrentFilter as String)
        Location: @Html.DropDownList("LocationList", "All")
        <input type="submit" value="Search" />
    </p>

}




<table class="table">
    <tr>
        @*<th>
            @Html.DisplayNameFor(model => model.LastName)
        </th>*@


        <th>
            @Html.ActionLink("Last Name", "Index", new { sortOrder = ViewBag.NameSort })
        </th>
  
        <th>
            @Html.DisplayNameFor(model => model.FirstName)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.Department)
        </th>
        
        
        
        @*<th>
            @Html.DisplayNameFor(model => model.Location)
        </th>*@
        
        
        <th>
            @Html.ActionLink("Location", "Index", new { sortOrder = ViewBag.LocationSort })
        </th>
  
        <th>
            @Html.DisplayNameFor(model => model.ExtensionInternal)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.ExtensionDirectDial)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.NumberMobile)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.NumberHome)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.NumberTextMessage)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.NumberOther)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.Terminated)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.Shift)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.Notes)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.NameGrouping)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.LastUpdatedDT)
        </th>
        <th></th>
    </tr>

@foreach (var item in Model) {
    <tr>
        <td>
            @Html.DisplayFor(modelItem => item.LastName)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.FirstName)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Department)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Location)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.ExtensionInternal)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.ExtensionDirectDial)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.NumberMobile)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.NumberHome)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.NumberTextMessage)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.NumberOther)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Terminated)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Shift)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Notes)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.NameGrouping)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.LastUpdatedDT)
        </td>
        <td>
            @Html.ActionLink("Edit", "Edit", new { id=item.RecordID }) |
            @Html.ActionLink("Details", "Details", new { id=item.RecordID }) |
            @Html.ActionLink("Delete", "Delete", new { id=item.RecordID })
        </td>
    </tr>
}

</table>
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 }) )

Open in new window


Controller:

public ActionResult Index(String sortOrder, string currentFilter, String LocationList, String SearchString, int? page )
        {


            //var pageIndex = (page ?? 1) - 1;
            //var pageSize = 10;
            //int totalUserCount;
                        
            //Added the page watch 
            ViewBag.CurrentSort = sortOrder;

            ViewBag.NameSort = String.IsNullOrEmpty(sortOrder) ? "Name_desc" : "";
            ViewBag.LocationSort = sortOrder == "Location" ? "Location_desc" : "Location";

            if (SearchString != null)
            {

             page = 1;
                
            }
            else {
                                
                SearchString = currentFilter;                 
             }
            ViewBag.CurrentFilter = SearchString;
            
                       
            var PhoneResults = from r in db.MasterLists
                               select r;
                     
         
            ViewBag.CurrentFilter = SearchString;

                switch (sortOrder){
               
                case "Location":
                    PhoneResults = PhoneResults.OrderBy(s => s.Location);
                    break;
                case "Location_desc":
                    PhoneResults = PhoneResults.OrderByDescending(s => s.Location);
                    break;
                case "Name_desc":
                    PhoneResults = PhoneResults.OrderByDescending(s => s.LastName);
                    break;
                default:
                    PhoneResults = PhoneResults.OrderBy(s => s.LastName);
                    break;
                }

                   
            var LocationListd = new List<String>();
            
            var LocationListQuerry = from r in db.MasterLists
                                     orderby r.Location
                                     select r.Location;

            LocationListd.AddRange(LocationListQuerry.Distinct());
                        ViewBag.LocationList = new SelectList(LocationListd);       
           
            if (!String.IsNullOrEmpty(SearchString)) {
                PhoneResults = PhoneResults.Where(s => s.LastName.ToUpper().Contains(SearchString.ToUpper()) ||
                     s.FirstName.ToUpper().Contains(SearchString.ToUpper()));
                            }

            if (!String.IsNullOrEmpty(LocationList)) { 
               PhoneResults = PhoneResults.Where(s => s.Location == LocationList);
                }

             //return View(db.MasterLists.ToList());
           //return View(PhoneResults.ToList());

           int pageSize = 3;
           int pageNumber = (page ?? 1);
           //
            return View(PhoneResults.ToPagedList(pageNumber, pageSize));
         
        
      }

Open in new window

0
 

Author Comment

by:yguyon28
ID: 40387247
Not sure what is wrong with this code....

  // GET: MasterLists
        public ActionResult Index(String sortOrder, string currentFilter, String LocationList, String SearchString, int? page )
        {


            //var pageIndex = (page ?? 1) - 1;
            //var pageSize = 10;
            //int totalUserCount;
                        
            //Added the page watch 
            ViewBag.CurrentSort = sortOrder;

            ViewBag.NameSort = String.IsNullOrEmpty(sortOrder) ? "Name_desc" : "";
            ViewBag.LocationSort = sortOrder == "Location" ? "Location_desc" : "Location";

            if (SearchString != null)
            {

             page = 1;
                
            }
            else {
                                
                SearchString = currentFilter;                 
             }
            ViewBag.CurrentFilter = SearchString;
            
                       
            var PhoneResults = from r in db.MasterLists
                               select r;
                     
         
            ViewBag.CurrentFilter = SearchString;

                switch (sortOrder){
               
                case "Location":
                    PhoneResults = PhoneResults.OrderBy(s => s.Location);
                    break;
                case "Location_desc":
                    PhoneResults = PhoneResults.OrderByDescending(s => s.Location);
                    break;
                case "Name_desc":
                    PhoneResults = PhoneResults.OrderByDescending(s => s.LastName);
                    break;
                default:
                    PhoneResults = PhoneResults.OrderBy(s => s.LastName);
                    break;
                }

                   
            var LocationListd = new List<String>();
            
            var LocationListQuerry = from r in db.MasterLists
                                     orderby r.Location
                                     select r.Location;

            LocationListd.AddRange(LocationListQuerry.Distinct());
                        ViewBag.LocationList = new SelectList(LocationListd);       
           
            if (!String.IsNullOrEmpty(SearchString)) {
                PhoneResults = PhoneResults.Where(s => s.LastName.ToUpper().Contains(SearchString.ToUpper()) ||
                     s.FirstName.ToUpper().Contains(SearchString.ToUpper()));
                            }

            if (!String.IsNullOrEmpty(LocationList)) { 
               PhoneResults = PhoneResults.Where(s => s.Location == LocationList);
                }

             //return View(db.MasterLists.ToList());
           //return View(PhoneResults.ToList());

           int pageSize = 10;
           int pageNumber = (page ?? 1);
           //
            return View(PhoneResults.ToPagedList(pageNumber, pageSize));
         
        
      }

Open in new window

0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question