Solved

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

Posted on 2014-10-17
5
1,416 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 74

Expert Comment

by:käµfm³d 👽
Comment Utility
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
Comment Utility
Why should I comment line 48-110, this will remove my header row... Does not work
0
 
LVL 74

Accepted Solution

by:
käµfm³d   👽 earned 500 total points
Comment Utility
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
Comment Utility
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
Comment Utility
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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

771 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now