@ Html.Partial导致类型错误

时间:2018-12-20 07:27:13

标签: c# asp.net-mvc

我有一个.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才能实现?

3 个答案:

答案 0 :(得分:1)

您需要将Controller的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>

答案 1 :(得分:1)

我将为此使用@ 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")

希望有帮助!

答案 2 :(得分:1)

您要从视图中调用控制器,以(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>

编辑:调整为在原始代码段中显示...