locked
Error When Running Application in the Partial View RRS feed

  • Question

  • User1807711795 posted

    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.

    Thursday, April 3, 2014 5:49 PM

Answers

  • User-760709272 posted

    You are passing no second param to the partial view so it is using the same Model as the parent view.  Your parent view's model is a List of movies so your partial view is given a List of movies, but your partial view's @model directive says it wants a single movie.  This code;

    <div id="divPartialView">
        @Html.Partial("PartialMovie")
    </div> 

    What are you expecting the partialmovie to show?  The first movie you have in the model?  The last one?  A random one?  Every one?  If you want to list every movie with a partial view showing each then you probably want code like this

    <div id="divPartialView">
        @foreach(Movie m in Model)
        {
            @Html.Partial("PartialMovie", m)
        }
    </div> 

    The above passes a single movie into the partial view, one at a time

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, April 8, 2014 8:37 AM

All replies

  • User1918509225 posted

    Hi vcampos2000,

    According to your post ,It seems that your controller  action passed a List<Movie> instance to your view which is strongly typed with ViewModels.Movies.

    The action should return a model of the same type declared on the first line of your view file,like code  below:

    View File:

    @model    IEnumerable<Movie>

    Controller File:

    public ActionResult Show(){
    return  View(Db.Moives. AsEnumerable());
    }
    

    So please check  the type on  the  view file which occur the exception and  the type on the action which return to the view are the same type.

    Hope it can help you.

    Best Regards,

    Kevin Shen.

    Tuesday, April 8, 2014 8:28 AM
  • User-760709272 posted

    You are passing no second param to the partial view so it is using the same Model as the parent view.  Your parent view's model is a List of movies so your partial view is given a List of movies, but your partial view's @model directive says it wants a single movie.  This code;

    <div id="divPartialView">
        @Html.Partial("PartialMovie")
    </div> 

    What are you expecting the partialmovie to show?  The first movie you have in the model?  The last one?  A random one?  Every one?  If you want to list every movie with a partial view showing each then you probably want code like this

    <div id="divPartialView">
        @foreach(Movie m in Model)
        {
            @Html.Partial("PartialMovie", m)
        }
    </div> 

    The above passes a single movie into the partial view, one at a time

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, April 8, 2014 8:37 AM