C# 将额外的 ViewData 传递给强类型的局部视图
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1169170/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Pass Additional ViewData to a Strongly-Typed Partial View
提问by Nathan Taylor
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?
我有一个强类型的 Partial View,它采用 ProductImage,当它被呈现时,我还想为它提供一些额外的 ViewData,我在包含页面中动态创建这些 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++;
}
采纳答案by womp
RenderPartial takes another parameter that is simply a ViewDataDictionary. You're almost there, just call it like this:
RenderPartial 采用另一个参数,它只是一个 ViewDataDictionary。你快到了,就这样称呼它:
Html.RenderPartial(
"ProductImageForm",
image,
new ViewDataDictionary { { "index", index } }
);
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.
请注意,这将覆盖默认情况下所有其他视图具有的默认 ViewData。如果您要向 ViewData 添加任何内容,则它不会出现在您传递给局部视图的这个新字典中。
回答by Joel Martinez
I think this should work no?
我认为这应该有效吗?
ViewData["currentIndex"] = index;
回答by griegs
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.
希望这可以帮助。如果没有,请告诉我,我会发布特定的代码。
回答by tvanfosson
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.
传递额外数据的最简单方法是将数据添加到视图的现有 ViewData 中,如@Joel Martinez 所述。但是,如果您不想污染 ViewData,RenderPartial 有一个方法,它接受三个参数以及您显示的两个参数版本。第三个参数是一个 ViewDataDictionary。您可以为您的部分构建一个单独的 ViewDataDictionary,其中仅包含您想要传入的额外数据。
回答by ctorx
To extend on what womp posted, you canpass new View Data while retaining the existing View Data if you use the constructor overload of the ViewDataDictionary
like so:
为了扩展 womp 发布的内容,如果您使用类似的构造函数重载,您可以传递新的视图数据,同时保留现有的视图数据ViewDataDictionary
:
Html.RenderPartial(
"ProductImageForm",
image,
new ViewDataDictionary(this.ViewData) { { "index", index } }
);
回答by muthuvel
@Html.Partial("_Header", new ViewDataDictionary { { "HeaderName", "User Management" }, { "TitleName", "List Of Users" } })
or
@{Html.RenderPartial("_Header", new ViewDataDictionary { { "HeaderName", "User Management" }, { "TitleName", "List Of Users" } });}
Partial Page(_Header):
部分页面(_Header):
<div class="row titleBlock">
<h1>@ViewData["HeaderName"].ToString()</h1>
<h5>@ViewData["TitleName"].ToString()</h5>
</div>
回答by Hyman1987
You can use the dynamic variable ViewBag
您可以使用动态变量 ViewBag
ViewBag.AnotherValue = valueToView;
回答by MithunChopda
This should also work.
这也应该有效。
this.ViewData.Add("index", index);
Html.RenderPartial(
"ProductImageForm",
image,
this.ViewData
);
回答by Drytin
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.
我知道这是一篇旧帖子,但我在使用 core 3.0 遇到类似问题时遇到了它,希望它对某人有所帮助。
@{
Layout = null;
ViewData["SampleString"] = "some string need in the partial";
}
<partial name="_Partial" for="PartialViewModel" view-data="ViewData" />