我试图在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>
原文地址:https://www.jb51.cc/aspnet/245231.html
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。