gpt4 book ai didi

asp.net-mvc-3 - MVC 3 使用 RenderPage 在 View 中更改模型

转载 作者:行者123 更新时间:2023-12-04 11:29:15 25 4
gpt4 key购买 nike

我在 MVC 3 中尝试更改 View 模型时遇到问题。

第一个 View ( 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>
}

第二个 View ( 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 传入 Frog 对象列表后,到达 @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 并将其内嵌在 的 foreach 循环中index.cshtml 结果是我所期望的。但是,这不会重用现有代码。

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

干杯!

最佳答案

像这样尝试:

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

关于asp.net-mvc-3 - MVC 3 使用 RenderPage 在 View 中更改模型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7653091/

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