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

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

发布时间:2020-12-15 20:59:29 所属栏目:asp.Net 来源:网络整理
导读:我试图在MVC 3中更改我的视图模型时遇到问题. 第一个视图(index.cshtml): @model IEnumerableMyProgram.MyFrogCollectionh1Welcome to my frog collection/h1@foreach(MyProgram.Frog frog in Model){ div class="frogDetails" @RenderPage("ShowFrogDetail
我试图在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>

(编辑:李大同)

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

    推荐文章
      热点阅读