传递到字典的模型产品A类的,但是这需要字典B型的模型项目 [英] The model item passed into the dictionary is of type A, but this dictionary requires a model item of type B

查看:187
本文介绍了传递到字典的模型产品A类的,但是这需要字典B型的模型项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好吧,还有一吨这样的问题,我已经通过看一下它1500lbs。我看到的那些,人们要么发出错误的类型或他们做了局部视图东西。我在我的情况做既不。所以,我确切的错误是:

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:

传递到字典中的模型产品类型ClanSite.Models.ViewModels.CategoryViewModel',但这需要字典类型的模型项目ClanSite.Models.ViewModels.UserLoginViewModel。

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'.

问题是,我有我的_Layout.cshtml(@model 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.

不过,在这些网页之一,我试图渲染类别的清单。我CategoryViewModel只包含的类别,GetCategories一个List()返回列表中。

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.

控制器

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

查看

@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>

当我尝试去到这个页面,我得到的错误。我通过与调试器的页面加强,并在被得到正确的数据:@ cat.CategoryInfo.Description

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

这真是困惑我,因为我是能够利用该模型没有任何问题的另一个页面上创建用户注册表单。所以,我怎么可以用一个模型在_layout并在视图中,我只是通过数据循环输出?

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?

推荐答案

我有在MVC应用程序也需要一个良好的使用模式和我的方法是不使用在模型中的 _Layout.cshtml 。如果有这样的情况下,像登录操作,需要在所有的页面,因此在 _Layout.cshtml ,一个的RenderPartial 调用应使用和特定的型号也应创建。

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>

所有的页面都提供,并影响其正常模型局部视图。然后可以创建并在 _Layout.cshtml 显示在 RenderBody()标记正常浏览没有任何型号的冲突。

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.

这篇关于传递到字典的模型产品A类的,但是这需要字典B型的模型项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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