I am trying to use Ajax navigation code to display paging information. The application is built using the MVC model with a ViewModel to store the paging information and the database is built by Code First.
When I run it I am gettint the following error:
The model item passed into the dictionary is of type 'System.Collections.Generic.List`1[MVCMovies_v13.Models.Movie]', but this dictionary requires a model item of type 'MVCMovies_v13.ViewModels.Movies`1[MVCMovies_v13.Models.Movie]'.
This where it ocurrs ( Specifically at @Html.Partial("PartialMovie") ):
@{
ViewBag.Title = "Movie Listings";
}<script type="text/javascript">
function linkClick(pageNumber) {$.ajax({
url: '@Url.Action("PartialMovie")',
data: { "pageNumber": pageNumber },
success: function (data) {$("#divPartialView").html(data);
}
});
}</script><h2>@ViewBag.Title</h2><div id="divPartialView">
@Html.Partial("PartialMovie")</div> Other view:
@model MVCMovies_v13.ViewModels.Movies<MVCMovies_v13.Models.Movie><!-- MVCMovies_v13.Models.Movie MVCMovies_v13.ViewModels.Movies --><table><thead><tr><th>MovieID</th><th>Title</th><th>Released</th></tr></thead><tbody>
@foreach (var movie in Model.Data)
{<tr><td>@movie.MovieId</td><td>@movie.Title</td><td>@movie.ReleaseYear</td></tr>
}</tbody><tfoot><tr><td colspan="4">
@for (int pgNum = 1; pgNum <= Model.NumberOfPages; pgNum++)
{
if (pgNum == Model.CurrentPage)
{
@pgNum
}
else
{<a onclick="linkClick(@pgNum)" href="javascript:void(0)")>@pgNum</a>
}
}</td></tr></tfoot></table>HomeControllr.cs (I am debugging a method in it)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MVCMovies_v13.Models;
using MVCMovies_v13.ViewModels;
using MVCMovies_v13.EFData;
namespace MVCMovies_v13.Controllers
{
public class HomeController : Controller
{
private MovieContext DB = new MovieContext();
//
// GET: /Home/
public ActionResult Index()
{
//Prmary View
return View(DB.moviesdb.ToList());
}
private const int PageSize = 5;
public ActionResult Student()
{
return View(GetMovies(1));
}
public ActionResult PartialMovie(int pageNumber = 1)
{
return PartialView(GetMovies(pageNumber));
}
private Movies<Movie> GetMovies(int pageNumber)
{
// Retrieve all movies and their properties from Movies DBdatabase
var movieToList = DB.moviesdb;
//movieToList = DB.moviesdb.OrderBy(p => p.Title).Skip(PageSize * (pageNumber - 1)).Take(PageSize).ToList();
//movieToList.NumberOfPages = Convert.ToInt32(Math.Ceiling(DB.moviesdb.Count() / PageSize));
//movieToList.CurrentPage = pageNumber;
//return movieToList;
return ViewBag.Test = "Test";
}
}
}
Here is the EFData:
using System;
using System.Collections.Generic;
using System.Web;
using System.Data.Entity;
using System.Data.Entity.ModelConfiguration.Conventions;
using MVCMovies_v13.Models;
namespace MVCMovies_v13.EFData
{
public class MovieContext : DbContext
{
public DbSet<Movie> moviesdb { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
}
}
}using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.Entity;
using MVCMovies_v13.Models;
namespace MVCMovies_v13.EFData
{
public class MovieInitializer : DropCreateDatabaseAlways<MovieContext>
{
protected override void Seed(MovieContext context)
{
var moviesListings = new List<Movie>
{
new Movie { MovieId = 1, Title = "Some Big Movie", ReleaseYear = 2013 },
};
moviesListings.ForEach(s => context.moviesdb.Add(s));
context.SaveChanges();
}
}
}I am lost at what to look at next.