加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-15 06:51:34 所属栏目:百科 来源:网络整理
导读:背景 尝试在ASP.NET MVC中呈现局部视图时,我收到以下错误.我是ASP.NET MVC的新手,我确信错误很简单,只是因为我缺乏完全的理解. 问题(对于那些不想阅读一切的人): 是什么导致这个错误? Exception Details: System.InvalidOperationException : The model it
背景

尝试在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.IEnumerable1[MyApp.Models.ClassroomFormViewModel]’`.

的entites

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

Classroom                   StickyNote 
------------                -----------
Id          1 -----         Id
Name                       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.IEnumerable1[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.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>

父视图

而来自其他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
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读