c# - MVC 给出了一个奇怪的错误

标签 c# asp.net-mvc linq

我得到的错误信息是:

The model item passed into the dictionary is of type 'System.Data.Linq.DataQuery`1[MvcApplication1.ContentPageNav]', but this dictionary requires a model item of type 'MvcApplication1.ContentPageNav'.

public ActionResult Edit()
{
   DataClasses1DataContext dc = new DataClasses1DataContext();
   var model = from m in dc.ContentPageNavs
      select m;
   return View(model);
}

关于我为什么会收到此错误的任何想法?任何帮助将不胜感激。

最佳答案

您正在选择 ContentPageNav列表到您的 model 变量中。

该 View 需要一个 ContentPageNav,而不是它们的列表。

试试这个:

var model = (from m in dc.ContentPageNavs
  select m).FirstOrDefault();

关于c# - MVC 给出了一个奇怪的错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5107579/

相关文章:

c# - .NET 核心 : Finally block not called on unhandled exception on Linux

c# - 如何使用日期范围从 Redis (pub/sub) 保存和检索 channel 数据?

c# - HttpClient中 "The server returned an invalid or unrecognized response"的含义

javascript - 我的 View 的相对 URL 路径在部署时中断

c# - 在 C# 中使用 Linq 开始从 excel 的特定行读取数据

c# - ReadBsonType 只能在 State 为 Type 时调用,不能在 State 为 Value 时调用

c# - 显示名称属性与显示属性

SQL RANDOM ORDER BY 连接表

LINQ 数据库调用次数

c# - 如何从列表列表创建新列表,其中元素在新列表中的顺序不同?