Click here to Skip to main content
15,891,375 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I'm learning ASP.Net MVC Core. While trying to call a function through a button click event, I'm getting this error:

InvalidOperationException: The model item passed into the ViewDataDictionary is of type 'Book_Store.Models.BookModel', but this ViewDataDictionary instance requires a model item of type 'System.Collections.Generic.IEnumerable`1[Book_Store.Models.BookModel]'.


I'm sort of confused what is the error. Isn't the data types same?

This is my BookModel

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace Book_Store.Models
{
    public class BookModel
    {
        public int Id { get; set; }
        public string Title { get; set; }
        public string Author { get; set; }
        public string Description { get; set; }
    }
}


This is the controller:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Book_Store.Models;
using Book_Store.Repository;
using Microsoft.AspNetCore.Mvc;

namespace Book_Store.Controllers
{
    public class BookController : Controller
    {
        private readonly BookRepository bookRepository = null;
        public BookController()
        {
            bookRepository = new BookRepository();
        }

        public ViewResult GetBookById(int id)  
        {
            var bookDetails = bookRepository.GetBookById(id);
            return View(bookDetails);
        }
        
    }
}


My repository where functions are defined along with the data is:

using Book_Store.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace Book_Store.Repository
{
    public class BookRepository
    {
        public List<BookModel> GetAllBooks()
        {
            return DataSource();
        }
        public BookModel GetBookById(int id)
        {
            return DataSource().Where(x => x.Id == id).FirstOrDefault();
        }
        public List<BookModel> SearchBook(string title, string authorName)
        {
            return DataSource().Where(x => x.Title.Contains(title) || x.Author.Contains(authorName)).ToList();
        }
        private List<BookModel> DataSource()
        {
            return new List<BookModel>
            {
                new BookModel(){Id=1, Author="Elif Shafaq", Title="Forty Rules of Love", Description="The pursuit for God"},
                new BookModel(){Id=2, Author="Stephen Hawkings", Title="The Grand Design", Description="Scientific view of the creation of universe"},
                new BookModel(){Id=3, Author="ABC", Title="MVC", Description="Basics of Model View Controller, Design Hierarchy"},
                new BookModel(){Id=4, Author="Author", Title="ASP.Net", Description="Web Development using modern web tools"},
                new BookModel(){Id=5, Author="DEF", Title="Cloud Computing", Description="A dive into new computing field"},
                new BookModel(){Id=6, Author="GHI", Title="Microsoft Azure", Description="Expand you business capabilities by leveraging Microsoft's servers"},
                new BookModel(){Id=7, Author="JKL", Title="Microsoft SQL", Description="Get to know one of the most popular RDBMS"},
                new BookModel(){Id=8, Author="MNO", Title="Xcode", Description="Unlock the capabilities of Apple's ecosystem"},
                new BookModel(){Id=9, Author="PQR", Title="Swift", Description="Make apps for macOS, iOS, tvOS, watchOS"}
            };
        }
    }
}


What I have tried:

I tried manually calling the function like this "https://localhost:5001/book/getbookbyid/1", here 1 is the id of book to retrieve but still getting the error:

An unhandled exception occurred while processing the request.

InvalidOperationException: The model item passed into the ViewDataDictionary is of type 'Book_Store.Models.BookModel', but this ViewDataDictionary instance requires a model item of type 'System.Collections.Generic.IEnumerable`1[Book_Store.Models.BookModel]'.
Posted
Updated 13-Nov-20 5:10am

1 solution

Your View will be using IEnumerable<BookModel> or List<BookModel> or something similar as the model, expecting a collection of BookModel items, but your controller is passing a single BookModel item to the view.

You need to either change the view to accept just BookModel as the model, or change the controller to return a collection of BookModel items. Which solution is right depends on what the view is doing. Given the name of the action I dare say it's the view you want to change to run off of a single item.
 
Share this answer
 
Comments
RaffaySajjad 13-Nov-20 11:57am    
Thank You. I overlooked the view. It is indeed using IEnumerable<bookmodel>, I changed it to BookModel.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900