【问题标题】:InvalidOperationException: The model item passed into the ViewDataDictionaryInvalidOperationException:传入 ViewDataDictionary 的模型项
【发布时间】:2022-01-19 02:07:13
【问题描述】:

我知道围绕这个主题有很多问题,但我尝试了不同的方法,但无法解决。所以我的列表返回码是这样的,这是 QuestionsController,model 是 Question

public async Task<IActionResult> Index()
{
     return View(await _context.Question.ToListAsync());
}

这是我的cshtml和模型标签

@model IEnumerable<SocialLearningCommunity.Models.Question>

错误

InvalidOperationException:传入的模型项 ViewDataDictionary 是类型 'System.Collections.Generic.List`1[SocialLearningCommunity.Models.Question]', 但是这个 ViewDataDictionary 实例需要一个类型的模型项 'SocialLearningCommunity.Models.User'。

数据类

public DbSet<User> User { get; set; }
public DbSet<Question> Question { get; set; }

谢谢。

【问题讨论】:

  • 如果您需要帮助,您也必须发布完整的索引视图。

标签: asp.net model-view-controller model


【解决方案1】:

你的索引视图有这行代码

@model SocialLearningCommunity.Models.User

但您正试图从控制器操作发送到视图

List <SocialLearningCommunity.Models.Question>

您必须决定必须使用哪种模型。或者您可以在问题视图中拥有问题模型,而不是在索引一中。或者您有用户模型的部分视图。

【讨论】:

  • 我解决了,问题是我在 Layout.cshtml 中有@model,它是共享的,所以我改变了它。
猜你喜欢
  • 2020-01-12
  • 2022-01-17
  • 2020-05-13
  • 2017-09-26
  • 2020-05-10
  • 1970-01-01
  • 2022-08-03
  • 2022-06-27
  • 2021-07-31
相关资源
最近更新 更多