Mark Tabor

Mark Tabor

  • 586
  • 2k
  • 464.4k

MVC Partial Views

Apr 3 2019 2:55 AM
I am following this below toturial for generting menu from Database
 
https://yogeshdotnet.com/dynamically-add-menu-submenus-asp-net-mvc-using-entity-framework/
 
it works fine for separate project but when i make the Partial view and calling it my code is not running below is my code of partial view and controller
 
_MenuPartial code:
  1. @model IEnumerable<Pakeeza_Estate.Models.tblMainMenu>  
  2. @{  
  3. ViewBag.Title = "Index";  
  4. Layout = "~/Views/Shared/_Layout.cshtml";  
  5. }  
  6. @*<link href="~/Content/themes/base/jquery-ui.css" rel="stylesheet" />*@  
  7. <link href="~/Content/bootstrap.css" rel="stylesheet" />  
  8. <script src="~/Scripts/jquery-3.3.1.js"></script>  
  9. <script src="~/Scripts/bootstrap.min.js"></script>  
  10. @*<script src="~/Scripts/jquery-ui-1.12.1.js"></script>*@  
  11. <h2>Index</h2>  
  12. <script>  
  13. $(document).ready(function(){  
  14. $('.nav li > .sub-menu').parent().hover(function () {  
  15. var submenu = $(this).children('.sub-menu');  
  16. if ($(submenu).is(':hidden')) {  
  17. $(submenu).slideDown(200);  
  18. else {  
  19. $(submenu).slideUp(200);  
  20. }  
  21. });  
  22. })  
  23. </script>  
  24. <style>  
  25. .nav li {  
  26. list-style: none;  
  27. padding: 5px 10px;  
  28. width: 300px;  
  29. }  
  30. .nav > li {  
  31. float: left;  
  32. }  
  33. .nav .sub-menu {  
  34. display: none;  
  35. }  
  36. </style>  
  37. <ul class="nav">  
  38. @{  
  39. foreach (var item in Model)  
  40. {  
  41. <li>  
  42. <a href="/Page/@item.MainMenu">@item.MainMenu</a>  
  43. @if (item.tblSubMenus.Count > 0)  
  44. {  
  45. <ul class="sub-menu">  
  46. @foreach (var item2 in item.tblSubMenus)  
  47. {  
  48. <li><a href="/Category/@item2.Controller/@item2.Id">@item2.SubMenu</a></li>  
  49. }  
  50. </ul>  
  51. }  
  52. </li>  
  53. }  
  54. }  
  55. </ul>  
  56. <h2>Index</h2>  
My Home controller :
  1. public PartialViewResult MenuPartial()  
  2. {  
  3. using (var db = new MenuModel())  
  4. {  
  5. var d = db.tblMainMenus.Include(c => c.tblSubMenus).ToList();  
  6. return PartialView("_MenuPartial",d);  
  7. }  
  8. }  
I need urgent response if someone can help me

Answers (3)