将附加 ViewData 传递给强类型部分视图
我有一个强类型的部分视图,它采用 ProductImage,当它呈现时,我还想为其提供一些我在包含页面中动态创建的附加 ViewData。 如何通过 RenderPartial 调用将强类型对象和自定义 ViewData 传递到部分视图?
var index = 0;
foreach (var image in Model.Images.OrderBy(p => p.Order))
{
Html.RenderPartial("ProductImageForm", image); // < Pass 'index' to partial
index++;
}
I have a strongly-typed Partial View that takes a ProductImage and when it is rendered I would also like to provide it with some additional ViewData which I create dynamically in the containing page. How can I pass both my strongly typed object and my custom ViewData to the partial view with the RenderPartial call?
var index = 0;
foreach (var image in Model.Images.OrderBy(p => p.Order))
{
Html.RenderPartial("ProductImageForm", image); // < Pass 'index' to partial
index++;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
您可以使用动态变量ViewBag
You can use the dynamic variable ViewBag
这也应该有效。
This should also work.
传递附加数据的最简单方法是将数据添加到视图的现有 ViewData 中,如 @Joel Martinez 所说。 但是,如果您不想污染 ViewData,RenderPartial 有一个方法,它接受三个参数以及您显示的两个参数版本。 第三个参数是 ViewDataDictionary。 您可以为您的部分构造一个单独的 ViewDataDictionary,其中仅包含您想要传入的额外数据。
The easiest way to pass additional data is to add the data to the existing ViewData for the view as @Joel Martinez notes. However, if you don't want to pollute your ViewData, RenderPartial has a method that takes three arguments as well as the two-argument version you show. The third argument is a ViewDataDictionary. You can construct a separate ViewDataDictionary just for your partial containing just the extra data that you want to pass in.
创建另一个包含强类型类的类。
将新内容添加到类中并将其返回到视图中。
然后在视图中,确保继承新类并更改现在出错的代码位。 即对您的字段的引用。
希望这可以帮助。 如果没有,请告诉我,我将发布具体代码。
Create another class which contains your strongly typed class.
Add your new stuff to the class and return it in the view.
Then in the view, ensure you inherit your new class and change the bits of code that will now be in error. namely the references to your fields.
Hope this helps. If not then let me know and I'll post specific code.
我知道这是一篇旧帖子,但我在使用 core 3.0 遇到类似问题时遇到了它,希望它对某人有所帮助。
I know this is an old post but I came across it when faced with a similar issue using core 3.0, hope it helps someone.
我认为这应该有效,不是吗?
I think this should work no?
部分页面(_Header):
Partial Page(_Header):
为了扩展 womp 发布的内容,如果您使用
ViewDataDictionary
的构造函数重载,您可以传递新的视图数据,同时保留现有的视图数据,如下所示:To extend on what womp posted, you can pass new View Data while retaining the existing View Data if you use the constructor overload of the
ViewDataDictionary
like so:RenderPartial 采用另一个参数,它只是一个 ViewDataDictionary。 就快到了,只需这样调用即可:
请注意,这将覆盖所有其他视图默认具有的默认 ViewData。 如果您要向 ViewData 添加任何内容,则它不会位于您传递给部分视图的新字典中。
RenderPartial takes another parameter that is simply a ViewDataDictionary. You're almost there, just call it like this:
Note that this will override the default ViewData that all your other Views have by default. If you are adding anything to ViewData, it will not be in this new dictionary that you're passing to your partial view.