My ASP.NET MVC app getting errored as below when trying to access data using Entity framework DATABASE FIRST..
Please let me know on what went wrong here
The model item passed into the dictionary is of type 'System.Linq.OrderedEnumerable`2[TESTInternetMVC.Product,System.String]', but this dictionary requires a model item of type 'System.Collections.Generic.IEnumerable`1[TESTInternetMVC.Models.Product]'.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.InvalidOperationException: The model item passed into the dictionary is of type 'System.Linq.OrderedEnumerable`2[TESTInternetMVC.Product,System.String]', but this dictionary requires a model item of type 'System.Collections.Generic.IEnumerable`1[TESTInternetMVC.Models.Product]'.
Source Error:
An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.
public class EFDFController : Controller { // // GET: /EFDF/
public ActionResult Index() { EFDFContext Ed = new EFDFContext(); IEnumerable prods = from p in Ed.Products.AsEnumerable() orderby p.price select new Product { id = p.id, productname = p.productname, price = p.price, description = p.description }; return View(prods);
//return View(); }
Model part
public partial class Product { [Key] public int id { get; set; } ///hidden [Required] public string productname { get; set; } [Required, DataType(DataType.Currency)] public decimal price { get; set; } [Required] public string description { get; set; }
}
From EDMX file
public partial class Product { public int id { get; set; } public string productname { get; set; } public decimal price { get; set; } public string description { get; set; } }
View part
@model IEnumerable
@{ ViewBag.Title = "Index"; }
Index
@Html.ActionLink("Create New", "Create")
@Html.DisplayNameFor(model => model.productname)
@Html.DisplayNameFor(model => model.price)
@Html.DisplayNameFor(model => model.description)
@foreach (var item in Model) {
@Html.DisplayFor(modelItem => item.productname)
@Html.DisplayFor(modelItem => item.price)
@Html.DisplayFor(modelItem => item.description)
@Html.ActionLink("Edit", "Edit", new { id=item.id }) | @Html.ActionLink("Details", "Details", new { id=item.id }) | @Html.ActionLink("Delete", "Delete", new { id=item.id })
}
3 Answers
Know the answer? Post it. Somebody with the same question will find it here.
Vincent Maverick DuranoRank #238 · 10.3K reputationPosted May 9, 2015, 2:04 AM EDT
Your view is specting a model with type IEnumerable
In your controller, make sure that you are returning the same type of model.
Sachin R82 reputationPosted May 9, 2015, 11:13 AM EDT
It worked for me..
Vincent Maverick DuranoRank #238 · 10.3K reputationPosted May 16, 2015, 2:48 AM EDT
Glad to be of help! :)
Just one favor, please don't forget to close this thread by marking the post which helps you as answer for the sake of future readers Thank you