asp.net-mvc-3 – MVC 3使用RenderPage更改视图中的模型

我试图在MVC 3中更改我的视图模型时遇到问题.

一个视图(index.cshtml):

@model IEnumerable<MyProgram.MyFrogCollection>

<h1>Welcome to my frog collection</h1>
@foreach(MyProgram.Frog frog in Model)
{
  <div class="frogDetails">
    @RenderPage("ShowFrogDetails.cshtml",frog);
  </div>
}

第二个视图(ShowFrogDetails.cshtml),我想在整个网站上使用:

@model MyProgram.Frog

<h3>Name:</h3><div class="detail">@Model.Name</div>
<h3>Colour:</h3><div class="detail">@Model.Colour</div>

但是,当我在传入青蛙对象列表后尝试运行页面index.cshtml时,在获取@RenderPage行时出现以下错误

Server Error in ‘/’ Application. The model item passed into the
dictionary is of type
‘System.Collections.Generic.List`1[MyProgram.Frog]’,but this
dictionary requires a model item of type ‘MyProgram.Frog’.

如果我要从ShowFrogDetails.cshtml中删除代码并将其放在index.cshtml的foreach循环内,那么结果就是我所期望的.但是,这不会重用现有代码.

无论如何我可以将模型更改为单个Frog对象以在RenderPage中使用吗?

干杯!

解决方法

试试这样:
<div class="frogDetails">
    @Html.Partial("ShowFrogDetails",frog)
</div>

相关文章

### 创建一个gRPC服务项目(grpc服务端)和一个 webapi项目(...
一、SiganlR 使用的协议类型 1.websocket即时通讯协议 2.Ser...
.Net 6 WebApi 项目 在Linux系统上 打包成Docker镜像,发布为...
一、 PD简介PowerDesigner 是一个集所有现代建模技术于一身的...
一、存储过程 存储过程就像数据库中运行的方法(函数) 优点:...
一、Ueditor的下载 1、百度编辑器下载地址:http://ueditor....