[英]Dictionary requires a model item of type System.Collections.Generic.List
I have a layout.csthml which contains a sidebar and also contains a @Renderbody. 我有一个layout.csthml,其中包含一个侧边栏,还包含一个@Renderbody。 Both the @Renderbody and the sidebar use the following in their individual views.
@Renderbody和侧边栏在其各自的视图中均使用以下内容。
@model List<appstowindows.Models.apps>
@foreach (var item in Model){...}
Both views render the list fine, but when I try to open the edit URL in the view I keep getting the error: 两种视图都可以很好地显示列表,但是当我尝试在视图中打开编辑URL时,仍然出现错误:
The model item passed into the dictionary is of type System.Data.Entity.DynamicProxies, Dictionary requires a model item of type System.Collections.Generic.List
传递到字典中的模型项的类型为System.Data.Entity.DynamicProxies,字典需要类型为System.Collections.Generic.List的模型项
Edit url 编辑网址
@Html.ActionLink("Edit", "Edit", new { id = item.app_key })
Index 指数
public ActionResult Index()
{
var applist = db.apps.Include(a => a.appgroups);
applist = db.apps.Include(a => a.appstatus);
return View(applist.ToList());
}
Edit 编辑
public ActionResult Edit(int? id)
{
apps apps = db.apps.Find(id);
return View(apps);
}
Important Note: If I would remove either one of the lists in one of the views the edit URL works fine. 重要说明:如果我要删除其中一个视图中的任一列表,则编辑URL可以正常工作。
What is causing this error and how to resolve? 是什么导致此错误以及如何解决?
update: 更新:
@model myproject.Models.apps
@{
ViewBag.Title = "Edit";
}
<h2>Edit</h2>
@using (Html.BeginForm("Edit", "Apps", null, FormMethod.Post, new { enctype = "multipart/form-data" }))
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
<h4>apps</h4>
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
@Html.HiddenFor(model => model.app_key)
</div>
}
You are passing model of wrong type to your view. 您正在将错误类型的模型传递给视图。 Passed object is of type
System.Data.Entity.DynamicProxies
while System.Collections.Generic.List
is required. 传递的对象的类型为
System.Data.Entity.DynamicProxies
而System.Collections.Generic.List
是必需的。 Check which object is provided as an argument to it. 检查提供哪个对象作为其参数。
UPDATED: And the problem is with model
specified at your layout page. 更新:问题出在您的布局页面上指定的
model
。 Edit
view is based on layout page, so it requires a model of type List<apps>
to be able to render. Edit
视图基于布局页面,因此它需要List<apps>
类型的模型才能呈现。 But Edit
view requires apps
as model on the opposite side. 但是“
Edit
视图”要求apps
在相反的一侧作为模型。 And while there is no type, which is List<apps>
and apps
at the same time, your code won't work. 虽然没有类型,即
List<apps>
和apps
同时存在,但是您的代码将无法工作。
Generally, you should avoid layout page typing, because all your views, based on that layout, will require the same or descendant type of model. 通常,应避免键入布局页面,因为基于该布局的所有视图都将需要相同或后代的模型类型。 If all of your pages should really share some information, you could solve this problem with multiple options:
如果您的所有页面都应该真正共享一些信息,则可以使用多种选项解决此问题:
async
(there are some hacks to make them async
, but it's not a case) async
(有一些技巧可以使它们async
,但事实并非如此) async
by default. async
的。 Also those blocks will not delay page loading and could be decorated with spinners or something in that way. In your case error is caused by @model List<appstowindows.Models.apps>
. 在您的情况下,错误是由
@model List<appstowindows.Models.apps>
引起的。 While Edit
view is executed, it renders layout page markup first, so object of type apps
, provided as model to Edit
view, is not suitable. 执行“
Edit
视图时,它首先呈现布局页面标记,因此不适合作为模型提供给“ Edit
视图的类型为apps
对象。 I suppose, you specified a type at your layout, because some markup is shared between all of your pages (if I'm wrong, just delete @model
from your layout, and everything will be okay) and this sharing could be implemented by one of methods I've proposed earlier. 我想,您在布局中指定了一种类型,因为在所有页面之间共享了一些标记(如果我错了,只需从布局中删除
@model
,一切都会好起来的),并且这种共享可以由一个人实现我之前提出的方法
If you go with the second one, you should add an action to your controller (don't know what information you are rendering, so I will call it Foo
) 如果您选择第二个,则应该向控制器添加一个动作(不知道您要渲染什么信息,因此我将其称为
Foo
)
public ActionResult Foo()
{
/*get data for your shared content part*/
return PartialView(/*provide data here*/);
}
Then create View
for it with markup, which should be shared. 然后为其创建带有标记的
View
,应将其共享。 (The one from your layout, which causes issue) (来自您布局的那一个,会引起问题)
@model List<appstowindows.Models.apps>
@foreach (var item in Model){...}
Then remove that markup from your layout and call @Html.Action("Foo") instead. 然后从您的布局中删除该标记,然后调用@ Html.Action(“ Foo”) 。
Btw, I suppose you should learn some information about layout pages and models in ASP.MVC, official website is a good starting point. 顺便说一句,我想您应该学习有关ASP.MVC中的布局页面和模型的一些信息,官方网站是一个很好的起点。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.