我有一个 .cshtml 文件需要呈现另一个 .cshtml 文件(菜单)。此菜单使用模型来决定显示哪些选项。当我从主 .cshtml 文件调用以呈现菜单时,它没有指出存在无效类型。这是错误信息:
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'.'
我在这里尝试了一些解决方案:The model item passed into the dictionary is of type .. but this dictionary requires a model item of type
我试过用
@Html.Partial("Menu", new MenuItemsViewModel())
它确实有效,但它正在创建一个新的 ViewModel,而不是调用我的方法从我的数据库中读取它。我有一个 Index.cshtml 文件,我在其中设置值,但在这里我只想从我的数据库中读取它。我还尝试在 MenuItemsController 中定义一个“GetViewModel”方法,创建一个新实例并调用该方法。它有效但感觉有点笨拙:@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>
}
从数据库中读取模型并填充模型。我是否需要为菜单创建一个新的 Controller 才能实现此目的?
最佳答案
您需要将 MenuItemsViewModel 的值从 Controller 传递到此 View 。
public ActionResult Index()
{
//Populate your MenuItemsViewModel from database here
MenuItemsViewModel menuItems = DB.GetMenuItems(); //Assuming GetMenuItems() is method which returns an object of type MenuItemsViewModel from database
ViewBag.MenuItems = menuItems;
return View();
}
然后在你的视野中
@using System.Web.Mvc.Html
@{
var menuItems = (MenuItemsViewModel)ViewBag.MenuItems;
}
<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", menuItems)
<header>
<div class="logo text-center">
<img src="~/Content/logo.png" />
</div>
</header>
<div>
@RenderBody()
</div>
</body>
关于c# - @Html.Partial 导致类型错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53864138/