Solved

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

Posted on 2014-10-17
5
1,827 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

[Webinar] How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

Question has a verified solution.

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

For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

724 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