【问题标题】:The model item passed into the dictionary is of type A, but this dictionary requires a model item of type B传入字典的模型项是 A 类型,但是这个字典需要 B 类型的模型项
【发布时间】:2012-06-11 01:49:54
【问题描述】:

好的,有很多这样的问题,我已经浏览了大约 1500 磅。我看到的那些,人们要么发送了错误的类型,要么他们在做一些偏颇的事情。在我的情况下,我都没有这样做。所以,我的确切错误是:

传入字典的模型项的类型为“ClanSite.Models.ViewModels.CategoryViewModel”,但此字典需要类型为“ClanSite.Models.ViewModels.UserLoginViewModel”的模型项。

问题是我的 _Layout.cshtml (@model ClanSite.Models.ViewModels.UserLoginViewModel) 上有一个模型,用于在每个页面上登录用户。

但是,在其中一个页面上,我正在尝试呈现类别列表。我的 CategoryViewModel 只包含一个 Category 的 List,GetCategories() 返回那个 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

这真的让我很困惑,因为我能够使用该模型在另一个页面上创建用于用户注册的表单而没有任何问题。那么,如何在 _Layout 和 View 中使用模型,而我只是在其中循环输出数据?

【问题讨论】:

  • 您的_Layout .cshtml 是否需要特定的@model 类型?
  • 我不太清楚你的意思。 _Layout.cshtml 里面确实有@model ClanSite.Models.ViewModels.UserLoginViewModel,用来提交表单来登录用户。
  • 我不认为你可以像这样混合指定的@model 类型......因此类型不匹配。您基本上是在要求视图引擎将您的 ClanSite.Models.ViewModels.CategoryViewModel 转换为 ClanSite.Models.ViewModels.UserLoginViewModel,这是它无法做到的。
  • 所以,试着让你的 _Layout 不是强类型的,看看是否可行;)
  • 那我该怎么做 _Layout 中的表格呢?另外,就像我说的那样,我可以在 _Layout 和我的注册页面上拥有表单,它使用 _Layout,还有另一个表单,_Layout 和 Registration 都使用不同的 ViewModel。

标签: asp.net-mvc-3 model views viewmodel


【解决方案1】:

我实际上能够很容易地解决这个问题。我刚刚制作了一个仅包含公共 UserLoginViewModel 成员的界面 ILayout。然后我在我的 CategoryViewModel 中实现这个接口。这意味着,我需要将 UserLoginViewModel 添加到 CategoryViewModel,但这根本不是问题。登录时我唯一需要更改的是,我没有从处理登录的 Action 将 UserLoginViewModel 发送到 View,而是发送了 ILayout。

【讨论】:

    【解决方案2】:

    我在 MVC 中确实有一个应用程序,它也需要很好地使用模型,我的方法是不在 _Layout.cshtml 中使用模型。如果存在这种情况,例如登录操作,所有页面都需要并因此在_Layout.cshtml 中定义,则应使用RenderPartial 调用,并且还应创建特定的模型。

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

    所有页面都将具有可用的局部视图并具有适当的模型。然后可以创建普通视图并将其显示在_Layout.cshtml 内的RenderBody() 标记中,而不会发生任何模型冲突。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-03-15
      • 2015-07-05
      相关资源
      最近更新 更多