Forums - C# Corner

Forum guidelines
Rohit Singh

Rohit Singh

  • 1.2k
  • 477
  • 18.2k

Getting error while loading values from database and display

Aug 19 2017 1:33 PM
Hello everyone, I want to read all the values from my table and display it on my view using code first approach of entity framework but i got an error in every try.

I have tried almost everything but couldn't achieve what I want..please solve this problem.. Review my Code Below:::

What I have tried:

This is My ActionMethod: in which i'm reading all the values from databsae.

public ActionResult ViewAll()
MenuRolesSample dbContext = new MenuRolesSample();
return View(dbContext.Menus.ToList());


This is my Viewpage: where I just want to display all the Names from the database table.

@model IEnumerable

ViewBag.Title = "ViewAll";
Layout = "~/Views/sahred/_layout.cshtml";
@foreach (var item in Model)




The Error which I've been facing is:

The model item passed into the dictionary is of type 'System.Collections.Generic.List`1[MenuRoles.Models.Menu]', but this dictionary requires a model item of type 'MenuRoles.Models.Menu'.

Answers (1)


Build smarter apps with Machine Learning, Bots, Cognitive Services - Start free.

Start Learning Now