在ASP.NET MVC中使用部分视图

Geo*_*ker 10 .net c# asp.net-mvc partial-views view

背景

尝试在ASP.NET MVC中呈现局部视图时,我收到以下错误.我是ASP.NET MVC的新手,我确信错误很容易解决,这源于我缺乏完整的理解.

问题(对于那些不想阅读所有内容的人):

导致此错误的原因是什么?

异常详细信息:: System.InvalidOperationException传递到字典中的模型项是类型, 'MyApp.Models.ClassroomFormViewModel' 但此字典需要类型为'System.Collections.Generic.IEnumerable1 的模型项 [MyApp.Models.ClassroomFormViewModel]'.


的entites

我有两个父/子关系的实体.

Classroom                   StickyNote 
------------                -----------
Id          1 -----         Id
Name               \        Name
(...)               \       Content
                     ---- * ClassroomID

模型

ModelStickyNote中,内容保存在不同的表中,并进行访问(使用Linq-to-SQL以下方法:

public IQueryable<StickyNote> GetStickyNotesByClassroom(Classroom classroom)
{
     return from stickynote in db.StickyNotes
            where stickynote.ClassroomID == classroom.ID
            select stickynote;
}
Run Code Online (Sandbox Code Playgroud)

错误

我创建了一个显示StickyNote内容的部分视图,因为它"属于"它所在的教室.我遇到的问题是我无法显示它,并收到以下错误:

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

异常详细信息:: System.InvalidOperationException传递到字典中的模型项是类型, 'MyApp.Models.ClassroomFormViewModel' 但此字典需要类型为'System.Collections.Generic.IEnumerable1 的模型项 [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 class="sticky_note_container">

<!-- actually use a post it note here on the page -->
<div class="sticky_note">
<div class="sticky_note_content">
<!-- content of sticky note here -->
<% Html.ActionLink(item.Name, "ShowStickyNoteContent"); %>
<!-- end of content of sticky note -->
</div>
</div>
<div class="sticky_note_footer">&nbsp;</div>
<br clear="all" />
</div>
         </td>
      <% } %>
     </tr>
   <% } %>
</table>
Run Code Online (Sandbox Code Playgroud)

父视图

来自另一个View的代码调用它:

<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits=
"System.Web.Mvc.ViewPage<MyApp.Models.ClassroomFormViewModel>" %>
{...}
  <% 
     Html.RenderPartial("StickyNotes", Model);
  %>
Run Code Online (Sandbox Code Playgroud)

Mic*_*cah 8

您正在将ClassroomFormViewModel的单个实例传入,而View正在等待一个集合即 IEnumerable<ClassroomFormViewModel>.

将PartialView中的声明更改为

Inherits="
System.Web.Mvc.ViewUserControl<MyApp.Models.ClassroomFormViewModel>"
Run Code Online (Sandbox Code Playgroud)

要么

你真正想要的(真正看到你的代码之后)是一个 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
}
Run Code Online (Sandbox Code Playgroud)