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]'.