【发布时间】:2014-06-24 06:40:27
【问题描述】:
给定一个视图模型类
public class Foo
{
public string Fuzz { get; set; }
public Bar Bar { get; set; }
}
public class Bar
{
public string Fizz { get; set; }
}
在控制器动作中,我将以下模型传递给视图:
View(new Foo { Fuzz = "Fizz", Bar = new Bar{ Fizz = "Fuzz" } });
在 Foo.cshtml 视图中
@model Foo
@Model.Fuzz
@{ Html.RenderPartial("BarPartial", Model.Bar); }
在视图局部BarPartial.cshtml
@model Bar
@Model.Fizz
抛出一个错误:
传入字典的模型项是 Foo 类型,但此字典需要 Bar 类型的模型项。
如何将父模型的属性传递给具有该属性类型的模型的局部视图?
【问题讨论】:
-
它应该完全按照您在此处显示的那样工作。你确定你在你的“真实代码”中是那样做的吗?
-
提示:除了
@{ Html.RenderPartial("BarPartial", Model.Bar); },你也可以使用更短的@Html.Partial("BarPartial", Model.Bar)。 -
感谢 chrfin,它实际上适用于一个新的 MVC5 项目。但是我收到此错误的其他项目一定有问题。
-
我试试这个,但你的代码对我来说很好。
标签: c# asp.net-mvc razor model-view-controller renderpartial