David Watson

David Watson

  • NA
  • 15
  • 2.2k

Cant see cause of model item passed into the ViewDataDictio

Mar 31 2017 7:39 PM
 Loverly error:
 
model item passed into the ViewDataDictionary is of type 'System.Collections.Generic.List`1[EVA.Models.Job]', but this ViewDataDictionary instance requires a model item of type 'Eva.PaginatedList`1[EVA.Models.Job]'.
 
Cant see why...Probably blind again.
 
Controller:
  1. public async Task<IActionResult> Index(  
  2.             string sortOrder,   
  3.             string currentFilter,  
  4.             string searchString,  
  5.             int? page)  
  6.         {  
  7.             ViewData["CurrentSort"] = sortOrder;  
  8.   
  9.             ViewData["NameSortParm"] = String.IsNullOrEmpty(sortOrder) ? "name_desc" : "";  
  10.             ViewData["DateSortParm"] = sortOrder == "Date" ? "date_desc" : "Date";  
  11.   
  12.             if (searchString != null)  
  13.             {  
  14.                 page = 1;  
  15.             }  
  16.             else  
  17.             {  
  18.                 searchString = currentFilter;  
  19.             }  
  20.   
  21.             ViewData["CurrentFilter"] = searchString;  
  22.   
  23.             var jobs = from j in _context.Job  
  24.                         .Include(j => j.Site)  
  25.                         .Include(j => j.WaterBody)  
  26.                        select j;  
  27.   
  28.             if (!String.IsNullOrEmpty(searchString))  
  29.             {  
  30.                 jobs = jobs.Where(s => s.Site.SiteName.Contains(searchString)  
  31.                                   || s.JobNumber.Contains(searchString));  
  32.             }  
  33.   
  34.             switch (sortOrder)  
  35.             {  
  36.                 case "name_desc":  
  37.                     jobs = jobs.OrderByDescending(j => j.Site.SiteName);  
  38.                     break;  
  39.   
  40.                 case "Date":  
  41.                     jobs = jobs.OrderBy(j => j.BookingDate);  
  42.                     break;  
  43.   
  44.                 case "date_desc":  
  45.                     jobs = jobs.OrderByDescending(j => j.BookingDate);  
  46.                     break;  
  47.   
  48.                 default:  
  49.                     jobs = jobs.OrderBy(j => j.Site.SiteName);  
  50.                     break;  
  51.             }  
  52.   
  53.             int pageSize = 9;  
  54.   
  55.             return View(await PaginatedList<Job>.CreateAsync(jobs.AsNoTracking(),page??1,pageSize ));  
  56.         }  
View:
 
  1. @model PaginatedList<EVA.Models.Job>  
  2.   
  3. @{  
  4.     ViewData["Title"] = "Jobs List";  
  5. }  
  6.   
  7. <h2>Jobs List</h2>  
  8.   
  9. <p>  
  10.     <a asp-action="Create">Create New</a>  
  11. </p>  
  12.   
  13. <form asp-action="Index" method="get">  
  14.     <div class="form-actions no-color">  
  15.         <p>  
  16.             Find by Site Name or by Job Number: <input type="text" name="SearchString" value="@ViewData["currentFilter"]" />  
  17.             <input type="submit" value="Search" class="btn btn-default" /> |  
  18.             <a asp-action="Index">Back to Full List</a>  
  19.         </p>  
  20.     </div>  
  21. </form>  
  22.   
  23.   
  24.   
  25. <table class="table">  
  26.     <thead>  
  27.         <tr>  
  28.             <th>  
  29.                  
  30.                 <a asp-action="Index" asp-route-sortOrder="@ViewData["DateSortParm"]">Booking Date</a>  
  31.              
  32.             </th>  
  33.             <th>  
  34.                Job Number  
  35.             </th>  
  36.             <th>  
  37.                   
  38.                 <a asp-action="Index" asp-route-sortOrder="@ViewData["NameSortParm"]">Site</a>  
  39.   
  40.                   
  41.             </th>  
  42.             <th>  
  43.                 Waterbody  
  44.             </th>  
  45.             <th>  
  46.                 Job Description  
  47.             </th>  
  48.               
  49.             <th></th>  
  50.         </tr>  
  51.     </thead>  
  52.     <tbody>  
  53. @foreach (var item in Model) {  
  54.         <tr>  
  55.             <td>  
  56.                 @Html.DisplayFor(modelItem => item.BookingDate)  
  57.             </td>  
  58.             <td>  
  59.                 @Html.DisplayFor(modelItem => item.JobNumber)  
  60.             </td>  
  61.             <td>  
  62.                 @Html.DisplayFor(modelItem => item.Site.SiteName)  
  63.             </td>  
  64.             <td>  
  65.                 @Html.DisplayFor(modelItem => item.WaterBody.WBName)  
  66.             </td>  
  67.             <td>  
  68.                 @Html.DisplayFor(modelItem => item.JobDescription)  
  69.             </td>  
  70.              
  71.             <td>  
  72.                 <a asp-action="Edit" asp-route-id="@item.JobID">Edit</a> |  
  73.                 <a asp-action="Details" asp-route-id="@item.JobID">Details</a> |  
  74.                 <a asp-action="Delete" asp-route-id="@item.JobID">Delete</a>  
  75.             </td>  
  76.         </tr>  
  77. }  
  78.     </tbody>  
  79. </table>  
  80.   
  81. @{  
  82.     var prevDisabled = !Model.HasPreviousPage ? "disabled" : "";  
  83.     var nextDisabled = !Model.HasNextPage ? "disabled" : "";  
  84. }  
  85.   
  86. <a asp-action="Index"  
  87.    asp-route-sortOrder="@ViewData["CurrentSort"]"  
  88.    asp-route-page="@(Model.PageIndex - 1)"  
  89.    asp-route-currentFilter="@ViewData["CurrentFilter"]"  
  90.    class="btn btn-default @prevDisabled btn">  
  91.     Previous  
  92. </a>  
  93. <a asp-action="Index"  
  94.    asp-route-sortOrder="@ViewData["CurrentSort"]"  
  95.    asp-route-page="@(Model.PageIndex + 1)"  
  96.    asp-route-currentFilter="@ViewData["CurrentFilter"]"  
  97.    class="btn btn-default @nextDisabled btn">  
  98.     Next  
  99. </a>  
I know its simple...but cant see where the error is coming from?