尝试在ASP.NET MVC中呈现局部视图时,我收到以下错误.我是ASP.NET MVC的新手,我确信错误很简单,只是因为我缺乏完全的理解.
问题(对于那些不想阅读一切的人):
是什么导致这个错误?
Exception Details:
system.invalIDOperationException
:
The model item passed into the
dictionary is of type
'MyApp.Models.ClassroomFormviewmodel'
but this dictionary requires a model
item of type
'System.Collections.Generic.IEnumerable
1[MyApp.Models.ClassroomFormviewmodel]’`.
的entites
我有两个具有父/子关系的实体.
Classroom stickynote ------------ -----------ID 1 ----- IDname \ name(...) \ Content ---- * ClassroomID
模型
在模型中,stickynote内容保存在不同的表中,并通过以下方法访问(使用linq-to-sql)
public Iqueryable<stickynote> GetstickynotesByClassroom(Classroom classroom){ return from stickynote in db.stickynotes where stickynote.ClassroomID == classroom.ID select stickynote;}
错误
我已经创建了一个局部视图来显示stickynote的内容,因为它属于它的教室.我遇到的问题是我无法让它显示,并收到以下错误:
The model item passed into the
dictionary is of type:
'MyApp.Models.ClassroomFormviewmodel'
but this dictionary requires a model
item of type
'System.Collections.Generic.IEnumerable
1[MyApp.Models.ClassroomFormviewmodel]’`.
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
'MyApp.Models.ClassroomFormviewmodel'
but this dictionary requires a model
item of type
'System.Collections.Generic.IEnumerable
1[MyApp.Models.ClassroomFormviewmodel]’`.
部分视图
这是部分视图代码:
<%@ Control Language="C#" inherits="System.Web.Mvc.VIEwUserControl<IEnumerable<MyApp.Models.ClassroomFormviewmodel>>" %> <table background="../../images/corkboard.jpg"> <% foreach (var items in Model) { %> <tr> <% foreach (var item in items.stickynotes) { %> <td><div ><!-- actually use a post it note here on the page --><div ><div ><!-- content of sticky note here --><% HTML.Actionlink(item.name,"ShowstickynoteContent"); %><!-- end of content of sticky note --></div></div><div > </div><br clear="all" /></div> </td> <% } %> </tr> <% } %></table>
父视图
而来自其他VIEw的代码调用它:
<%@ Page title="" Language="C#" MasterPagefile="~/VIEws/Shared/Site.Master" inherits="System.Web.Mvc.VIEwPage<MyApp.Models.ClassroomFormviewmodel>" %>{...} <% HTML.RenderPartial("stickynotes",Model); %>解决方法 您正在将ClassroomFormviewmodel的一个实例传递到VIEw中,并且VIEw正期待一个集合,即IEnumerable< ClassroomFormviewmodel>.
将您的PartialVIEw中的声明更改为
inherits="System.Web.Mvc.VIEwUserControl<MyApp.Models.ClassroomFormviewmodel>"
要么
你真正想要的(真正看你的代码)是一个IEnumerable< ClassroomFormviewmodel>
所以您的呼叫页面中的模型需要是IEnumerable< ClassroomFormviewmodel>
从本质上讲,你正在努力做到这一点
public voID Render(ClassroomFormviewmodel model){ RenderPartial(model) //Cannot cast single instance into an IEnumerable}public string RenderPartial(IEnumerable<ClassroomFormviewmodel> model){ //Do something}总结
以上是内存溢出为你收集整理的c# – 在ASP.NET MVC中使用部分视图全部内容,希望文章能够帮你解决c# – 在ASP.NET MVC中使用部分视图所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)