sonal malhotra

sonal malhotra

  • NA
  • 410
  • 51.9k

does not work with include method (model)in mvc

Feb 25 2019 4:47 AM
hi everyone ,
 
i am giving u code here below
 
controller
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Data;  
  4. using System.Data.Entity;  
  5. using System.Linq;  
  6. using System.Net;  
  7. using System.Web;  
  8. using System.Web.Mvc;  
  9. using onlineshopping_sonal.Models;  
  10. public ActionResult Index()  
  11. {  
  12. //var itemtable = db.Itemtables.ToList();  
  13. var items = db.Itemtables.Include(i => i.categoryTable).Include(i => i.prodtbl);  
  14. return View(items.ToList());  
  15. }  
index.cshtml
  1. @model IEnumerable<onlineshopping_sonal.Models.Itemtable>  
  2. @{  
  3. ViewBag.Title = "Index";  
  4. Layout = "~/Views/Shared/_Layout.cshtml";  
  5. }  
  6. <h2>Index</h2>  
  7. <p>  
  8. @Html.ActionLink("Create New", "Create")  
  9. </p>  
  10. <table class="table">  
  11. <tr>  
  12. <th>  
  13. @Html.DisplayNameFor(model => model.categoryTable.CategoryName)  
  14. </th>  
  15. <th>  
  16. @Html.DisplayNameFor(model => model.prodtbl.Productname)  
  17. </th>  
  18. <th>  
  19. @Html.DisplayNameFor(model => model.Title)  
  20. </th>  
  21. <th>  
  22. @Html.DisplayNameFor(model => model.Price)  
  23. </th>  
  24. @*<th>  
  25. @Html.DisplayNameFor(model => model.ItemUrl)  
  26. </th>*@  
  27. <th></th>  
  28. </tr>  
  29. @foreach (var item in Model) {  
  30. <tr>  
  31. <td>  
  32. @Html.DisplayFor(modelItem => item.categoryTable.CategoryName)  
  33. </td>  
  34. <td>  
  35. @Html.DisplayFor(modelItem => item.prodtbl.Productname)  
  36. </td>  
  37. <td>  
  38. @Html.DisplayFor(modelItem => item.Title)  
  39. </td>  
  40. <td>  
  41. @Html.DisplayFor(modelItem => item.Price)  
  42. </td>  
  43. @*<td>  
  44. @Html.DisplayFor(modelItem => item.ItemUrl)  
  45. </td>*@  
  46. <td>  
  47. @Html.ActionLink("Edit", "Edit", new { id=item.ItemId }) |  
  48. @Html.ActionLink("Details", "Details", new { id=item.ItemId }) |  
  49. @Html.ActionLink("Delete", "Delete", new { id=item.ItemId })  
  50. </td>  
  51. </tr>  
  52. }  
  53. </table>  
error shown below
 
Server Error in '/' Application.
A specified Include path is not valid. The EntityType 'shop_cartModel.Itemtable' does not declare a navigation property with the name 'categoryTable'.
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: A specified Include path is not valid. The EntityType 'shop_cartModel.Itemtable' does not declare a navigation property with the name 'categoryTable'.
Source Error:
Line 22: //var itemtable = db.Itemtables.ToList(); Line 23: var items = db.Itemtables.Include(i => i.categoryTable).Include(i => i.prodtbl); Line 24: return View(items.ToList()); Line 25: Line 26: }
Source File: C:\Users\admin\documents\visual studio 2015\Projects\onlineshopping_sonal\onlineshopping_sonal\Controllers\StoreManagerController.cs Line: 24
Stack Trace:
 
... any idea ? any soulution to us?

Answers (2)