Solved

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

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

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…
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.
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

911 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

23 Experts available now in Live!

Get 1:1 Help Now