The model item passed into the dictionary is of type A, but this dictionary requires a model item of type B

妖精的绣舞 提交于 2019-12-24 03:19:13

问题


Ok, there are a ton of questions like this and I've looked through about 1500lbs of it. The ones that I saw, people were either sending the wrong type or they were doing something with a partial view. I am doing neither in my case. So, my exact error is:

The model item passed into the dictionary is of type 'ClanSite.Models.ViewModels.CategoryViewModel', but this dictionary requires a model item of type 'ClanSite.Models.ViewModels.UserLoginViewModel'.

The issue is that I have a model on my _Layout.cshtml (@model ClanSite.Models.ViewModels.UserLoginViewModel) that is used to login in the user on every page.

But, on one of those pages, I am trying to render a list of Categories. My CategoryViewModel only contains a List of Category, GetCategories() returns that List.

Controller

public ActionResult Categories()
    {
        CategoryViewModel cats = new CategoryViewModel();
        try
        {
            cats.Categories = ForumQueries.GetCategories();
        }
        catch
        {
            return RedirectToAction("Message", new { msg = "categories" });
        }
        return View(cats);
    }

View

@model ClanSite.Models.ViewModels.CategoryViewModel
@{
    ViewBag.Title = "clanSite - Categories";
}
<div class="forumPostTable">
@foreach (ClanSite.Models.Tables.Join.Category cat in Model.Categories)
{
    <div class="forumPostTableRow cursorPointer" onclick="linkTo('@Url.Action("Index", "Home")')">
        <div class="forumCategoryTableCellTitle">
            <div class="forumCategoryTitle">
                <a href="" class="linkNoDecGold">Title</a>
            </div>
            <div class="forumCategoryTitleDesc">
                @cat.CategoryInfo.Description
            </div>
        </div>
    </div>
}
</div>

When I try to go to this page, I get the error. I stepped through the page with the debugger and was getting the correct data in: @cat.CategoryInfo.Description

It's really confusing me because I was able to create a form for user registration on another page using the model without any issues. So, how can I use a model in the _Layout and in a View in which I am just looping through the data for output?


回答1:


I do have an application in MVC that also requires a good use of Models and my approach would be not to use a Model in the _Layout.cshtml. If there is such a case, like Login operations, needed in all pages and therefore defined in the _Layout.cshtml, a RenderPartial call should be used and a specific Model should be also created.

<section id="login">
    @{ Html.RenderPartial("_Login", new MyProjectName.Models.Account.LoginModel()); }
</section>

All the pages will have the Partial View available and with its proper Model. Normal Views can then be created and displayed in the RenderBody() tag inside _Layout.cshtml without any Model conflicts.




回答2:


I was actually able to get around this rather easily. I just made an interface ILayout that only contains a public UserLoginViewModel member. I then implement this interface in my CategoryViewModel. Which means, I need to add UserLoginViewModel to CategoryViewModel, but this is not an issue at all. The only thing I needed to change for logging in, was instead of sending UserLoginViewModel to the View from the Action that handles the logging in, I sent ILayout.



来源:https://stackoverflow.com/questions/10924507/the-model-item-passed-into-the-dictionary-is-of-type-a-but-this-dictionary-requ

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!