传递到字典中的模型项的类型为'system.collections.generic.list`1 [alohomora.models.interviewquestions]',但此字典需要类型为'alohomora.models.user'的模型项 [英] The model item passed into the dictionary is of type 'system.collections.generic.list`1[alohomora.models.interviewquestions]', but this dictionary requires a model item of type 'alohomora.models.user'

查看:246
本文介绍了传递到字典中的模型项的类型为'system.collections.generic.list`1 [alohomora.models.interviewquestions]',但此字典需要类型为'alohomora.models.user'的模型项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

这是我在MVC5应用程序中定义的模型:





我尝试过:



这是我的行动

 public ActionResult CQuestions()
{
using(UserAccountContext userAccount = new UserAccountContext())
{
return View(userAccount.InterviewQuestions.ToList< interviewquestions>());
}
}



这是我的观点

 @ model List< alohomora.models.interviewquestions> 
@ {
ViewBag.Title =CQuestions;
}









这是例外

传递到字典中的模型项的类型为'System.Collections.Generic.List`1 [AlohoMora.Models.InterviewQuestions]',但此字典需要类型的模型项'AlohoMora.Models.User'。 
描述:执行当前Web请求期间发生了未处理的异常。请查看堆栈跟踪以获取有关错误及其源自代码的位置的更多信息。

异常详细信息:System.InvalidOperationException:传递到字典中的模型项的类型为'System.Collections.Generic.List`1 [AlohoMora.Models.InterviewQuestions]',但此字典需要一个模型'AlohoMora.Models.User'类型的项目。

源错误:

在执行当前Web请求期间生成了未处理的异常。可以使用下面的异常堆栈跟踪来识别有关异常的起源和位置的信息。

堆栈跟踪:

解决方案

我要做的是首先扩展控制器以执行故障排除;然后在返回值之前插入断点,以便您可以准确查看返回的内容。

  public  ActionResult CQuestions(){
List questions = new List< interviewquestions>();

使用(UserAccountContext userAccount = new UserAccountContext()){
questions = userAccount.InterviewQuestions.ToList< interviewquestions>();
}
return 查看(问题); // 在此行的开头插入断点
}


Hi all,

Here is my Model defined in MVC5 application:



What I have tried:

This is my action

public ActionResult CQuestions()
        {
            using (UserAccountContext userAccount = new UserAccountContext())
            {
                return View(userAccount.InterviewQuestions.ToList<interviewquestions>());
            }
        }


This is my view

@model List<alohomora.models.interviewquestions>
@{
    ViewBag.Title = "CQuestions";
}





This is the exception

The model item passed into the dictionary is of type 'System.Collections.Generic.List`1[AlohoMora.Models.InterviewQuestions]', but this dictionary requires a model item of type 'AlohoMora.Models.User'.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 

Exception Details: System.InvalidOperationException: The model item passed into the dictionary is of type 'System.Collections.Generic.List`1[AlohoMora.Models.InterviewQuestions]', but this dictionary requires a model item of type 'AlohoMora.Models.User'.

Source Error: 

An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.

Stack Trace:

解决方案

What I would do is to start by expanding out the controller to do your troubleshooting; and then insert a breakpoint just before returning the value so that you can see exactly what is being returned.

public ActionResult CQuestions() {
  List questions = new List<interviewquestions>();

  using (UserAccountContext userAccount = new UserAccountContext()) {
    questions = userAccount.InterviewQuestions.ToList<interviewquestions>();
  }
  return View(questions);  // insert breakpoint at start of this line
}


这篇关于传递到字典中的模型项的类型为'system.collections.generic.list`1 [alohomora.models.interviewquestions]',但此字典需要类型为'alohomora.models.user'的模型项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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