我知道圍繞這個主題有很多問題,但我嘗試了不同的方法并且無法解決。所以我的串列回傳代碼是這樣的,這是 QuestionsController,模型是 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; }
謝謝。
uj5u.com熱心網友回復:
你的索引視圖有這行代碼
@model SocialLearningCommunity.Models.User
但是您正在嘗試從控制器操作發送到視圖
List <SocialLearningCommunity.Models.Question>
您必須決定必須使用哪種模型。或者您可以在問題視圖中使用問題模型,而不是在索引一中。或者你有一個帶有 User 模型的區域視圖。
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/383889.html
