[英]@Html.Partial causes type errors
我有一个.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'.'
我在这里尝试了一些解决方案: 传入字典的模型项是类型..但是这个字典需要一个类型的模型项
我试过用了
@Html.Partial("Menu", new MenuItemsViewModel())
哪个工作但它正在创建一个新的ViewModel,而不是调用我的方法从我的数据库中读取它。 我有一个Index.cshtml文件,我正在设置值,但这里我想做的就是从我的数据库中读取它。 我还尝试在MenuItemsController中定义“GetViewModel”方法,创建一个新实例并调用该方法。 它有效,但感觉有点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>
}
从数据库中读取模型并填充模型。 我是否需要为菜单创建一个新的控制器才能实现此目的?
您需要将MenuItemsViewModel的值从控制器传递到此视图。
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>
我会使用@ Html.Action(我正在更新Priyank Panchal的代码):
public ActionResult GetMenu()
{
//Populate your MenuItemsViewModel from database here
MenuItemsViewModel menuItems = DB.GetMenuItems(); //Assuming GetMenuItems() is method which returns an object of type MenuItemsViewModel from database
return PartialView("Menu", menuItems);
}
在视图中,而不是:
@Html.Partial("Menu", menuItems)
使用以下内容:
@Html.Action("GetMenu", "YourController")
希望能帮助到你 !
您希望从视图中调用控制器,以便(a)从数据库中获取值,并(b)在菜单局部视图中渲染它们,以便...
控制器:
public ActionResult Menu() {
//get stuff from the db
return PartialView(menuModel); //this would be the partial view for your menu
}
'主'视图:
<head>
...
</head>
<body>
@{ Html.RenderAction("Menu", "YourController"); }
<header>
<div class="logo text-center">
<img src="~/Content/logo.png" />
</div>
</header>
<div>
@RenderBody()
</div>
</body>
编辑:调整以显示在您的原始代码段内...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.