TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
Answers
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Forums
Monthly Leaders
Forum guidelines
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:
public
async Task<IActionResult> Index(
string
sortOrder,
string
currentFilter,
string
searchString,
int
? page)
{
ViewData[
"CurrentSort"
] = sortOrder;
ViewData[
"NameSortParm"
] = String.IsNullOrEmpty(sortOrder) ?
"name_desc"
:
""
;
ViewData[
"DateSortParm"
] = sortOrder ==
"Date"
?
"date_desc"
:
"Date"
;
if
(searchString !=
null
)
{
page = 1;
}
else
{
searchString = currentFilter;
}
ViewData[
"CurrentFilter"
] = searchString;
var jobs = from j
in
_context.Job
.Include(j => j.Site)
.Include(j => j.WaterBody)
select j;
if
(!String.IsNullOrEmpty(searchString))
{
jobs = jobs.Where(s => s.Site.SiteName.Contains(searchString)
|| s.JobNumber.Contains(searchString));
}
switch
(sortOrder)
{
case
"name_desc"
:
jobs = jobs.OrderByDescending(j => j.Site.SiteName);
break
;
case
"Date"
:
jobs = jobs.OrderBy(j => j.BookingDate);
break
;
case
"date_desc"
:
jobs = jobs.OrderByDescending(j => j.BookingDate);
break
;
default
:
jobs = jobs.OrderBy(j => j.Site.SiteName);
break
;
}
int
pageSize = 9;
return
View(await PaginatedList<Job>.CreateAsync(jobs.AsNoTracking(),page??1,pageSize ));
}
View:
@model PaginatedList<EVA.Models.Job>
@{
ViewData[
"Title"
] =
"Jobs List"
;
}
<h2>Jobs List</h2>
<p>
<a asp-action=
"Create"
>Create New</a>
</p>
<form asp-action=
"Index"
method=
"get"
>
<div
class
=
"form-actions no-color"
>
<p>
Find by Site Name or by Job Number: <input type=
"text"
name=
"SearchString"
value=
"@ViewData["
currentFilter
"]"
/>
<input type=
"submit"
value=
"Search"
class
=
"btn btn-default"
/> |
<a asp-action=
"Index"
>Back to Full List</a>
</p>
</div>
</form>
<table
class
=
"table"
>
<thead>
<tr>
<th>
<a asp-action=
"Index"
asp-route-sortOrder=
"@ViewData["
DateSortParm
"]"
>Booking Date</a>
</th>
<th>
Job Number
</th>
<th>
<a asp-action=
"Index"
asp-route-sortOrder=
"@ViewData["
NameSortParm
"]"
>Site</a>
</th>
<th>
Waterbody
</th>
<th>
Job Description
</th>
<th></th>
</tr>
</thead>
<tbody>
@
foreach
(var item
in
Model) {
<tr>
<td>
@Html.DisplayFor(modelItem => item.BookingDate)
</td>
<td>
@Html.DisplayFor(modelItem => item.JobNumber)
</td>
<td>
@Html.DisplayFor(modelItem => item.Site.SiteName)
</td>
<td>
@Html.DisplayFor(modelItem => item.WaterBody.WBName)
</td>
<td>
@Html.DisplayFor(modelItem => item.JobDescription)
</td>
<td>
<a asp-action=
"Edit"
asp-route-id=
"@item.JobID"
>Edit</a> |
<a asp-action=
"Details"
asp-route-id=
"@item.JobID"
>Details</a> |
<a asp-action=
"Delete"
asp-route-id=
"@item.JobID"
>Delete</a>
</td>
</tr>
}
</tbody>
</table>
@{
var prevDisabled = !Model.HasPreviousPage ?
"disabled"
:
""
;
var nextDisabled = !Model.HasNextPage ?
"disabled"
:
""
;
}
<a asp-action=
"Index"
asp-route-sortOrder=
"@ViewData["
CurrentSort
"]"
asp-route-page=
"@(Model.PageIndex - 1)"
asp-route-currentFilter=
"@ViewData["
CurrentFilter
"]"
class
=
"btn btn-default @prevDisabled btn"
>
Previous
</a>
<a asp-action=
"Index"
asp-route-sortOrder=
"@ViewData["
CurrentSort
"]"
asp-route-page=
"@(Model.PageIndex + 1)"
asp-route-currentFilter=
"@ViewData["
CurrentFilter
"]"
class
=
"btn btn-default @nextDisabled btn"
>
Next
</a>
I know its simple...but cant see where the error is coming from?
Reply
Answers (
0
)
jQuery DataTables - Can't bind data
Access the label inside DetailsView