首页 文章

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

提问于
浏览
10

背景

尝试在 ASP.NET MVC 中渲染局部视图时,我收到以下错误。我是 ASP.NET MVC 的新手,我确信这个错误很容易解决,只是因为我缺乏完整的理解。

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

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

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


****的 entites

我有两个 parent/child 关系的实体。

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

模型

Model中,StickyNote 内容保存在不同的表中,并通过以下方法访问(使用Linq-to-SQL

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

错误

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

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

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

局部视图

这是局部视图代码:

<%@ 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>

家长视图

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

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

2 回答

  • 8

    您正在将 ClassroomFormViewModel 的单个实例传入,并且 View 期望一个集合 i.e。 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
    }
    
  • 2

    你的部分应该开始

    <%@ Control Language="C#" Inherits="
    System.Web.Mvc.ViewUserControl<MyApp.Models.ClassroomFormViewModel>" >
    

    我想你想在你的页面上显示一个教室。如果要显示更多,请不要使用视图模型列表。使用一个包含教室列表的视图模型

相关问题