如何使用 razor 实现动态嵌套 _Layout?
是否可以有动态嵌套_layout页面?
这是我得到的:
_ShopDetailsLayout.cshtml (动态嵌套 _layout):
@model MVC.Web.Areas.Shop.Models.ShopDetailViewModel
@{
Layout = "~/Views/Shared/_Layout.cshtml";
}
Credits.cshtml:
@model MVC.Web.Areas.Shop.Models.CreditsViewModel
@{
ViewBag.Title = "Credits";
Layout = "~/Areas/Game/Views/Shared/_ShopDetailsLayout.cshtml";
}
我遇到的问题是其他页面(如 Credits.cshtml )使用嵌套布局不具有相同的 @model 所以我收到此错误:
模型项传入 字典的类型是 'MVC.Web.Areas.Shop.Models.CreditsViewModel', 但这本字典需要一个模型 项目类型 'MVC.Web.Areas.Shop.Models.ShopDetailViewModel'。
我希望任何人都可以提供帮助
Is it possible to have dynamic nested _layout pages ?
Here's what I got:
_ShopDetailsLayout.cshtml (Dynamic nested _layout):
@model MVC.Web.Areas.Shop.Models.ShopDetailViewModel
@{
Layout = "~/Views/Shared/_Layout.cshtml";
}
Credits.cshtml:
@model MVC.Web.Areas.Shop.Models.CreditsViewModel
@{
ViewBag.Title = "Credits";
Layout = "~/Areas/Game/Views/Shared/_ShopDetailsLayout.cshtml";
}
the problem i am having is that the other pages, like Credits.cshtml is using the nested layout does not have the same @model so I get this error:
The model item passed into the
dictionary is of type
'MVC.Web.Areas.Shop.Models.CreditsViewModel',
but this dictionary requires a model
item of type
'MVC.Web.Areas.Shop.Models.ShopDetailViewModel'.
I hope anyone can help
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要更改布局以与实际模型兼容。
您可能想要使用通用的基本类型,或者坚持使用默认的
动态
模型。You need to change your layouts to be compatible with the actual model.
You may want to use a common base type, or stick with the default
dynamic
model.