C# 字典需要System.Collections.Generic.List类型的模型项
我有一个layout.csthml,它包含一个侧栏,还包含一个@Renderbody。@Renderbody和侧边栏在各自的视图中都使用以下内容C# 字典需要System.Collections.Generic.List类型的模型项,c#,asp.net-mvc,razor,C#,Asp.net Mvc,Razor,我有一个layout.csthml,它包含一个侧栏,还包含一个@Renderbody。@Renderbody和侧边栏在各自的视图中都使用以下内容 @model List<appstowindows.Models.apps> @foreach (var item in Model){...} 索引 编辑 重要提示:如果我要删除其中一个视图中的任何一个列表,则编辑URL工作正常 导致此错误的原因是什么以及如何解决 更新: @model myproject.Models.apps @{
@model List<appstowindows.Models.apps>
@foreach (var item in Model){...}
索引
编辑
重要提示:如果我要删除其中一个视图中的任何一个列表,则编辑URL工作正常
导致此错误的原因是什么以及如何解决
更新:
@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>
}
@model myproject.Models.apps
@{
ViewBag.Title=“编辑”;
}
编辑
@使用(Html.BeginForm(“Edit”、“Apps”、null、FormMethod.Post、new{enctype=“multipart/formdata”}))
{
@Html.AntiForgeryToken()
应用程序
@Html.ValidationSummary(true,“,new{@class=“text danger”})
@Html.HiddenFor(model=>model.app_键)
}
您将错误类型的模型传递给视图。传递的对象的类型为System.Data.Entity.DynamicProxies
,而System.Collections.Generic.List
是必需的。检查作为参数提供给它的对象
更新:
而问题在于布局页面中指定的型号
<代码>编辑视图基于布局页面,因此它需要类型为<代码>列表的模型
顺便说一句,我想你应该在ASP.MVC中了解一些关于布局页面和模型的信息,正式版是一个很好的起点。如果我删除其中一个列表,效果会很好,这个答案对我没有帮助。在
Edit
视图上引发异常,对吗db.apps.Find(id)
返回apps
类型System.Data.Entity.DynamicProxies
的类后代,它是错误的。你能展示你的编辑查看标记吗?谢谢你的回答,尽管我不确定我是否理解。你能给出一个关于这个问题的代码示例吗。你知道为什么我只使用一个列表就可以了吗?在我的答案中添加了更多的信息。它适用于索引
操作,因为模型与布局页面中的模型相同。(我不确定,我明白你在说什么列表)在布局中你称之为索引操作?你能提交你的布局和侧边栏吗?
public ActionResult Index()
{
var applist = db.apps.Include(a => a.appgroups);
applist = db.apps.Include(a => a.appstatus);
return View(applist.ToList());
}
public ActionResult Edit(int? id)
{
apps apps = db.apps.Find(id);
return View(apps);
}
@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>
}
public ActionResult Foo()
{
/*get data for your shared content part*/
return PartialView(/*provide data here*/);
}
@model List<appstowindows.Models.apps>
@foreach (var item in Model){...}