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

我试图在MVC 3中更改我的视图模型时遇到问题. 第一个视图(index.cshtml): @model IEnumerableMyProgram.MyFrogCollectionh1Welcome to my frog collection/h1@foreach(MyProgram.Frog frog in Model){ div class=frogDetails

我试图在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>

作者: dawei

【声明】:滨州站长网内容转载自互联网,其相关言论仅代表作者个人观点绝非权威,不代表本站立场。如您发现内容存在版权问题,请提交相关链接至邮箱:bqsm@foxmail.com,我们将及时予以处理。

为您推荐

联系我们

联系我们

0577-28828765

在线咨询: QQ交谈

邮箱: xwei067@foxmail.com

工作时间:周一至周五,9:00-17:30,节假日休息

返回顶部