I would be interested to see a specific code example of the problem you're facing, for context, if you have one?
Going on what you've said, you are right to be suspicious that you've been forced into using the ViewBag as it's not really something we should be using if we can help it. Obviously it's there and can be a good "get out of jail free card", but sticking to view models is certainly better.
Have you considered defining and using sections in your layout/view? They essentially work like a named version of RenderBody.
For example, in your Layout you might have:
@RenderSection("SideBar", required: false)
This can be accessed from your view, like this:
@model your.namespace.viewmodel
... some view cshtml ...
@section SideBar {
<h4>Related content for @Model.Name</h4>
...
}
Check out this overview of sections, it includes more in depth examples and also how to define default section content on the occasions you don't want to pass anything.
发布评论
评论(1)
我有兴趣看到您所面临的问题的特定代码示例,如果您有一个问题?
说一下您所说的话,您对被迫使用
ViewBag
的可疑是正确的,因为如果我们可以帮助它,这并不是我们应该使用的东西。显然,它在那里,并且可以是一个很好的“脱离监狱卡”,但是坚持查看模型肯定会更好。您是否考虑过在布局/视图中定义和使用部分?它们本质上像
Renderbody
的命名版本一样工作。例如,在您的布局中,您可能会有:
可以从您的视图中访问,例如:
查看此章节概述,它包含更多深度示例,以及如何在您不想传递任何内容的情况下定义默认的部分内容。
I would be interested to see a specific code example of the problem you're facing, for context, if you have one?
Going on what you've said, you are right to be suspicious that you've been forced into using the
ViewBag
as it's not really something we should be using if we can help it. Obviously it's there and can be a good "get out of jail free card", but sticking to view models is certainly better.Have you considered defining and using sections in your layout/view? They essentially work like a named version of
RenderBody
.For example, in your Layout you might have:
This can be accessed from your view, like this:
Check out this overview of sections, it includes more in depth examples and also how to define default section content on the occasions you don't want to pass anything.