gpt4 book ai didi

c# - 在 ASP.NET MVC 中使用分部 View

转载 作者:可可西里 更新时间:2023-11-01 09:08:40 26 4
gpt4 key购买 nike

背景

尝试在 ASP.NET MVC 中呈现局部 View 时收到以下错误。我是 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]'`.


实体

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

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

Model

In the Model the StickyNote Content is kept in a different table, and accessed (using Linq-to-SQL through the following method:

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

错误

我创建了一个部分 View 来显示 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]'`.

局部 View

这是部分 View 代码:

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

以及调用它的另一个 View 中的代码:

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

最佳答案

您正在将 ClassroomFormViewModel 的单个实例传入,并且 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 中使用分部 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1103977/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com