I have a .cshtml file that needs to render another .cshtml file (menu). This menu uses a model in order to decide what options to show. When I call from the main .cshtml file to the render the menu, it fails saying that there are invalid types. This is the error message:
System.InvalidOperationException: 'The model item passed into the dictionary is of type 'System.Collections.Generic.List`1[XXXXX.ScheduleItem]', but this dictionary requires a model item of type 'XXXXXX.Models.MenuItemsViewModel'.'
I've tried some of the solutions here: The model item passed into the dictionary is of type .. but this dictionary requires a model item of type
I've tried using
@Html.Partial("Menu", new MenuItemsViewModel())
Which does work but it is creating a new ViewModel, not calling my method for reading it from my database. I have a Index.cshtml file where I am setting the value though, but here all I wanna do is read it from my database. I also tried defining a "GetViewModel" method in the MenuItemsController, create a new instance and calling that method. It works but feels kinda haxy: @Html.Partial("Menu", new MenuItemsBackofficeController().GetViewModel())
@using System.Web.Mvc.Html
<head>
<!-- Required meta tags -->
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<!-- Bootstrap CSS -->
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.1/css/bootstrap.min.css" integrity="sha384-WskhaSGFgHYWDcbwN70/dfYBj47jz9qbsMId/iRN3ewGhXQFZCSftd1LZCfmhktB" crossorigin="anonymous">
<link rel="stylesheet" type="text/css" href="~/Styles/fonts.css" />
<link rel="stylesheet" type="text/css" href="~/Styles/style.css" />
<title>@ViewBag.Title -</title>
</head>
<body>
@Html.Partial("Menu", new MenuItemsViewModel())
<header>
<div class="logo text-center">
<img src="~/Content/logo.png" />
</div>
</header>
<div>
@RenderBody()
</div>
</body>
@model XXXX.Models.MenuItemsViewModel
@if (User.Identity.IsAuthenticated)
{
<nav class="navbar navbar-expand-lg navbar-dark bg-dark custom-menu">
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span> Meny
</button>
<div class="collapse navbar-collapse" id="navbarSupportedContent">
<ul class="navbar-nav mr-auto">
@if (Model.Schedule)
{
<li class="nav-item"><a class="nav-link" href="@Url.Action("Schedule", "Home")">Schema</a></li>
}
</ul>
</div>
</nav>
}
To read the model from the database and populate the model. Do I need to create a new Controller for the Menu to achive this?