传递给的模型项的类型为'LoginModel',但是此实例需要类型为'HomeViewModel'的模型项 [英] The model item passed into the is of type 'LoginModel', but this instance requires a model item of type 'HomeViewModel'

查看:50
本文介绍了传递给的模型项的类型为'LoginModel',但是此实例需要类型为'HomeViewModel'的模型项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到的错误的全文

处理请求时发生未处理的异常.InvalidOperationException:传递到ViewDataDictionary的模型项的类型为'FashionNova.Web.Areas.Identity.Pages.Account.LoginModel',但是此ViewDataDictionary实例需要类型为'FashionNova.Web.ViewModels.HomeViewModel'的模型项.>

An unhandled exception occurred while processing the request. InvalidOperationException: The model item passed into the ViewDataDictionary is of type 'FashionNova.Web.Areas.Identity.Pages.Account.LoginModel', but this ViewDataDictionary instance requires a model item of type 'FashionNova.Web.ViewModels.HomeViewModel'.

这是我的HomeController

This is my HomeController

namespace FashionNova.Web.Controllers
{
    using FashionNova.Services;
    using FashionNova.Web.ViewModels;
    using Microsoft.AspNetCore.Mvc;

    public class HomeController : BaseController
    {
        private readonly IMainCategoryService mainCategoryService;

        public HomeController(IMainCategoryService mainCategoryService)
        {
            this.mainCategoryService = mainCategoryService;
        }

        public IActionResult Index(HomeViewModel model)
        {
            var categoriesViewModel = this.mainCategoryService
                .GetAllMainCategories<HomeMainCategoryViewModel>();

            model.AllCategoriesViewModel = categoriesViewModel;

            return this.View(model);
        }

        public IActionResult Privacy()
        {
            return this.View();
        }

        public IActionResult About()
        {
            return this.View();
        }

        public IActionResult Contact()
        {
            return this.View();
        }

        public IActionResult Test()
        {
            return this.View();
        }

        [ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
        public IActionResult Error() => this.View();
    }
}

这是我的布局

@using FashionNova.Data.Models
@using Microsoft.AspNetCore.Identity

@inject SignInManager<ApplicationUser> SignInManager
@inject UserManager<ApplicationUser> UserManager


<!DOCTYPE html>
<html lang="en">
<head>
    <title>@ViewData["Title"]</title>
    <meta name="description" content="Fashion Nova is the top online fashion store for women & men. Shop sexy club dresses, jeans, shoes, bodysuits, skirts & more. Cheap & affordable fashion online">
    <!-- Favicon -->
    <link rel="icon" type="image/x-icon" href="images/icons/favicon.ico">
    <!-- Plugins CSS File -->
    <link rel="stylesheet" href="~/css/bootstrap.min.css">
    <!-- Main CSS File -->
    <link rel="stylesheet" href="~/css/style.min.css">
</head>
<body>
<div class="page-wrapper">
    <header class="header">
        <partial name="Partials/_HeaderTopPartial"/>
        <div class="header-middle sticky-header">
            <div class="container">
                <div class="header-left">
                    <ul class="menu sf-arrows">
                        <li class="active">
                            <a asp-area="" asp-controller="Home" asp-action="Index">Home</a>
                        </li>
                        <partial name="Partials/NavigationPartials/_NavigationHeaderLeftCategoryPartial"/>
                        <partial name="Partials/NavigationPartials/_NavigationHeaderLeftProductsPartial"/>
                        <partial name="Partials/NavigationPartials/_NavigationHeaderLeftPagesPartial"/>
                    </ul>
                </div>
                <partial name="Partials/NavigationPartials/_NavigationHeaderCenterPartial"/>
                <div class="header-right">
                    <partial name="Partials/NavigationPartials/_NavigationHeaderRightPartial"/>
                </div><!-- End .header-right -->
            </div><!-- End .container -->
        </div><!-- End .header-middle -->
    </header><!-- End .header -->

    <main class="main">
        @this.RenderBody()
    </main>
    <partial name="Partials/_FooterPartial"/>
</div>

<div class="mobile-menu-overlay"></div>
<div class="mobile-menu-container">
    <partial name="Partials/_MobileMenuPartial"/>
</div>

<a id="scroll-top" href="#top" title="Top" role="button">
    <i class="icon-angle-up"></i>
</a>

<script src="~/js/jquery.min.js"></script>
<script src="~/js/bootstrap.bundle.min.js"></script>
<script src="~/js/plugins.min.js"></script>
<script src="~/js/main.min.js"></script>
@this.RenderSection("Scripts", required: false)
</body>
</html>

一切正常,直到我尝试将模型传递给布局中的任何局部,无论是页脚局部,页眉局部,导航局部.

Everything works normal, until I try to pass a model to ANY of the partials in the layout, be it footer partial, header partial, navigation partial.

我将HomeViewModel传递给Navigation部分的Category部分,因此我不会弄乱导航的其他部分

I am passing the HomeViewModel, to the Category part of the Navigation partial, so I don't mess with the other parts of the navigation

@model HomeViewModel
<li>
    <a href="category.html" class="sf-with-ul">Categories</a>
    <div class="megamenu megamenu-fixed-width">
        <div class="row">
            <div class="col-lg-8">
                <div class="row">
                    <div class="col-lg-6">
                        <div class="menu-title">
                            <a href="#">Variations 1<span class="tip tip-new">New!</span></a>
                        </div>
                        <ul>
                            <li>
                                <a href="category-banner-full-width.html">Fullwidth Banner<span class="tip tip-hot">Hot!</span></a>
                            </li>
                            <li>
                                <a href="category-banner-boxed-slider.html">Boxed Slider Banner</a>
                            </li>
                            <li>
                                <a href="category-banner-boxed-image.html">Boxed Image Banner</a>
                            </li>
                            <li>
                                <a href="category-sidebar-left.html">Left Sidebar</a>
                            </li>
                            <li>
                                <a href="category-sidebar-right.html">Right Sidebar</a>
                            </li>
                            <li>
                                <a href="category-flex-grid.html">Product Flex Grid</a>
                            </li>
                            <li>
                                <a href="category-horizontal-filter1.html">Horizontal Filter1</a>
                            </li>
                            <li>
                                <a href="category-horizontal-filter2.html">Horizontal Filter2</a>
                            </li>
                        </ul>
                    </div><!-- End .col-lg-6 -->
                    <div class="col-lg-6">
                        <div class="menu-title">
                            <a href="#">Variations 2</a>
                        </div>
                        <ul>
                            <li>
                                <a href="#">Product List Item Types</a>
                            </li>
                            <li>
                                <a href="category-infinite-scroll.html">Ajax Infinite Scroll</a>
                            </li>
                            <li>
                                <a href="category-3col.html">3 Columns Products</a>
                            </li>
                            <li>
                                <a href="category-4col.html">4 Columns Products <span class="tip tip-new">New</span></a>
                            </li>
                            <li>
                                <a href="category-5col.html">5 Columns Products</a>
                            </li>
                            <li>
                                <a href="category-6col.html">6 Columns Products</a>
                            </li>
                            <li>
                                <a href="category-7col.html">7 Columns Products</a>
                            </li>
                            <li>
                                <a href="category-8col.html">8 Columns Products</a>
                            </li>
                        </ul>
                    </div><!-- End .col-lg-6 -->
                </div><!-- End .row -->
            </div><!-- End .col-lg-8 -->
            <div class="col-lg-4">
                <div class="banner">
                    <a href="#">
                        <img src="~/images/menu-banner-2.jpg" alt="Menu banner">
                    </a>
                </div><!-- End .banner -->
            </div><!-- End .col-lg-4 -->
        </div>
    </div><!-- End .megamenu -->
</li>

我的loginPartial包含在导航部分的另一部分中,我试图将它们分开,以便不会传递错误的模型,但即使在那之后-还会出现相同的问题.

My loginPartial is contained in another part of the navigation partial, I tried to divide them so the wrong model doesnt get passed, but even after that - the same issue comes back.

<li>
    <a href="#" class="sf-with-ul">Pages</a>
    <ul>
        <li>
            <a href="cart.html">Shopping Cart</a>
        </li>
        <li>
            <a href="#">Checkout</a>
            <ul>
                <li>
                    <a href="checkout-shipping.html">Checkout Shipping</a>
                </li>
                <li>
                    <a href="checkout-shipping-2.html">Checkout Shipping 2</a>
                </li>
                <li>
                    <a href="checkout-review.html">Checkout Review</a>
                </li>
            </ul>
        </li>
        <li>
            <a href="#">Dashboard</a>
            <ul>
                <li>
                    <a href="dashboard.html">Dashboard</a>
                </li>
                <li>
                    <a href="my-account.html">My Account</a>
                </li>
            </ul>
        </li>
        <li>
            <a asp-area="" asp-controller="Home" asp-action="About">About Us</a>
        </li>
        <li>
            <a href="#">Blog</a>
            <ul>
                <li>
                    <a href="blog.html">Blog</a>
                </li>
                <li>
                    <a href="single.html">Blog Post</a>
                </li>
            </ul>
        </li>
        <li>
            <a asp-area="" asp-controller="Home" asp-action="Contact">Contact Us</a>
        </li>
        <li>
            <partial name="_LoginPartial"/>
        </li>
        <li>
            <a a asp-area="Identity" asp-page="/Account/ForgotPassword">Forgot Password</a>
        </li>
    </ul>
</li>

我正在将HomeViewModel传递给Index,因为我想在首页加载所有类别,产品和2行产品,但是当我这样做时-登录被破坏了.

I am passing the HomeViewModel to Index, cause I want to load all the categories, products and 2 rows of products in the front page, but when I do it - the login gets broken.

推荐答案

您的局部视图期望使用类型为 HomeViewModel 的模型,但是当您从_Layout.cshtml引用局部视图时,您未指定它,因此该页面默认情况下会将父模型"发送到您的局部视图,这会导致您遇到错误.

Your partial view is expecting a model of type HomeViewModel but you did not specify it when referencing the partial view from your _Layout.cshtml, hence the page by default sends the Parent Model to your partial view which causes the error you are having.

您需要通过预期的模型,如下所示:

You need to pass the expected model like below:

@await Html.PartialAsync("_myPartial",new HomeViewModel())

更新:

如果要在_Layout.cshtml中将数据传递到部分视图,请参见以下示例:

If you want to pass data to partial view in _Layout.cshtml,here is a smiple demo like below:

1._Layout.cshtml:

1._Layout.cshtml:

@await Html.PartialAsync("_myPartial",new HomeViewModel() { Id=1,Name="aa"})

2._myPartial.cshtml:

2._myPartial.cshtml:

@model HomeViewModel
<table class="table">
    <thead>
        <tr>
            <th>
                @Html.DisplayNameFor(model => model.Id)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.Name)
            </th>
        </tr>
    </thead>
    <tbody>            
        <tr>
            <td>
                @Html.DisplayFor(model => model.Id)
            </td>
            <td>
                @Html.DisplayFor(model => model.Name)
            </td>
        </tr>            
    </tbody>
</table>

这篇关于传递给的模型项的类型为'LoginModel',但是此实例需要类型为'HomeViewModel'的模型项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆