?
Solved

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

Posted on 2014-10-17
5
Medium Priority
?
2,000 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 2000 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

Python: Series & Data Frames With Pandas

Learn the basics of Python’s pandas library of series & data frames and how we can use these tools for data manipulation.

Question has a verified solution.

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

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
Suggested Courses

770 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