Sachin R

Sachin R

  • NA
  • 91
  • 6.8k

Issue in using Entity Framework Database first in ASP.NET MV

May 9 2015 2:51 AM

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.

Stack Trace:
[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]'.]    System.Web.Mvc.ViewDataDictionary`1.SetModel(Object value) +378    System.Web.Mvc.ViewDataDictionary.set_Model(Object value) +47    System.Web.Mvc.ViewDataDictionary..ctor(ViewDataDictionary dictionary) +614    System.Web.Mvc.ViewDataDictionary`1..ctor(ViewDataDictionary viewDataDictionary) +37    System.Web.Mvc.WebViewPage`1.SetViewData(ViewDataDictionary viewData) +98    System.Web.Mvc.WebViewPage.set_ViewData(ViewDataDictionary value) +39    System.Web.Mvc.RazorView.RenderView(ViewContext viewContext, TextWriter writer, Object instance) +425    System.Web.Mvc.BuildManagerCompiledView.Render(ViewContext viewContext, TextWriter writer) +382    System.Web.Mvc.ViewResultBase.ExecuteResult(ControllerContext context) +431    System.Web.Mvc.ControllerActionInvoker.InvokeActionResult(ControllerContext controllerContext, ActionResult actionResult) +39    System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilterRecursive(IList`1 filters, Int32 filterIndex, ResultExecutingContext preContext, ControllerContext controllerContext, ActionResult actionResult) +116    System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilterRecursive(IList`1 filters, Int32 filterIndex, ResultExecutingContext preContext, ControllerContext controllerContext, ActionResult actionResult) +529    System.Web.Mvc.ControllerActionInvoker.InvokeActionResultWithFilters(ControllerContext controllerContext, IList`1 filters, ActionResult actionResult) +106    System.Web.Mvc.Async.<>c__DisplayClass2b.<BeginInvokeAction>b__1c() +321    System.Web.Mvc.Async.<>c__DisplayClass21.<BeginInvokeAction>b__1e(IAsyncResult asyncResult) +185    System.Web.Mvc.Async.WrappedAsyncResult`1.CallEndDelegate(IAsyncResult asyncResult) +42    System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +133    System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +56    System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeAction(IAsyncResult asyncResult) +40    System.Web.Mvc.Controller.<BeginExecuteCore>b__1d(IAsyncResult asyncResult, ExecuteCoreState innerState) +34    System.Web.Mvc.Async.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult) +70    System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +139    System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +59    System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +40    System.Web.Mvc.Controller.EndExecuteCore(IAsyncResult asyncResult) +44    System.Web.Mvc.Controller.<BeginExecute>b__15(IAsyncResult asyncResult, Controller controller) +39    System.Web.Mvc.Async.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult) +62    System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +139    System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +59    System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +40    System.Web.Mvc.Controller.EndExecute(IAsyncResult asyncResult) +39    System.Web.Mvc.Controller.System.Web.Mvc.Async.IAsyncController.EndExecute(IAsyncResult asyncResult) +39    System.Web.Mvc.MvcHandler.<BeginProcessRequest>b__5(IAsyncResult asyncResult, ProcessRequestState innerState) +39    System.Web.Mvc.Async.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult) +70    System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +139    System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +59    System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +40    System.Web.Mvc.MvcHandler.EndProcessRequest(IAsyncResult asyncResult) +40    System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.EndProcessRequest(IAsyncResult result) +38    System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +9651116    System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +155 

Controller part

public class EFDFController : Controller
{
//
// GET: /EFDF/

public ActionResult Index()
{
EFDFContext Ed = new EFDFContext();
IEnumerable<Product> 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<TESTInternetMVC.Models.EFDF.Product>

@{
ViewBag.Title = "Index";
}

<h2>Index</h2>

<p>
@Html.ActionLink("Create New", "Create")
</p>
<table>
<tr>
<th>
@Html.DisplayNameFor(model => model.productname)
</th>
<th>
@Html.DisplayNameFor(model => model.price)
</th>
<th>
@Html.DisplayNameFor(model => model.description)
</th>
<th></th>
</tr>

@foreach (var item in Model) {
<tr>
<td>
@Html.DisplayFor(modelItem => item.productname)
</td>
<td>
@Html.DisplayFor(modelItem => item.price)
</td>
<td>
@Html.DisplayFor(modelItem => item.description)
</td>
<td>
@Html.ActionLink("Edit", "Edit", new { id=item.id }) |
@Html.ActionLink("Details", "Details", new { id=item.id }) |
@Html.ActionLink("Delete", "Delete", new { id=item.id })
</td>
</tr>
}

</table>

Answers (3)