将附加 ViewData 传递给强类型部分视图

发布于 2024-07-27 23:15:14 字数 325 浏览 8 评论 0原文

我有一个强类型的部分视图,它采用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

难得心□动 2024-08-03 23:15:25

您可以使用动态变量ViewBag

ViewBag.AnotherValue = valueToView;

You can use the dynamic variable ViewBag

ViewBag.AnotherValue = valueToView;
弱骨蛰伏 2024-08-03 23:15:24

这也应该有效。

this.ViewData.Add("index", index);

Html.RenderPartial(
      "ProductImageForm", 
       image, 
       this.ViewData
); 

This should also work.

this.ViewData.Add("index", index);

Html.RenderPartial(
      "ProductImageForm", 
       image, 
       this.ViewData
); 
征棹 2024-08-03 23:15:23

传递附加数据的最简单方法是将数据添加到视图的现有 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.

客…行舟 2024-08-03 23:15:22

创建另一个包含强类型类的类。

将新内容添加到类中并将其返回到视图中。

然后在视图中,确保继承新类并更改现在出错的代码位。 即对您的字段的引用。

希望这可以帮助。 如果没有,请告诉我,我将发布具体代码。

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.

爱要勇敢去追 2024-08-03 23:15:21

我知道这是一篇旧帖子,但我在使用 core 3.0 遇到类似问题时遇到了它,希望它对某人有所帮助。

@{
Layout = null;
ViewData["SampleString"] = "some string need in the partial";
}

<partial name="_Partial" for="PartialViewModel" view-data="ViewData" />

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.

@{
Layout = null;
ViewData["SampleString"] = "some string need in the partial";
}

<partial name="_Partial" for="PartialViewModel" view-data="ViewData" />
淡水深流 2024-08-03 23:15:20

我认为这应该有效,不是吗?

ViewData["currentIndex"] = index;

I think this should work no?

ViewData["currentIndex"] = index;
痕至 2024-08-03 23:15:19
@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" } });}

部分页面(_Header):

<div class="row titleBlock">
    <h1>@ViewData["HeaderName"].ToString()</h1>
    <h5>@ViewData["TitleName"].ToString()</h5>
</div>
@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):

<div class="row titleBlock">
    <h1>@ViewData["HeaderName"].ToString()</h1>
    <h5>@ViewData["TitleName"].ToString()</h5>
</div>
骷髅 2024-08-03 23:15:19

为了扩展 womp 发布的内容,如果您使用 ViewDataDictionary 的构造函数重载,您可以传递新的视图数据,同时保留现有的视图数据,如下所示:

Html.RenderPartial(
      "ProductImageForm", 
       image, 
       new ViewDataDictionary(this.ViewData) { { "index", index } }
); 

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:

Html.RenderPartial(
      "ProductImageForm", 
       image, 
       new ViewDataDictionary(this.ViewData) { { "index", index } }
); 
烟花易冷人易散 2024-08-03 23:15:18

RenderPartial 采用另一个参数,它只是一个 ViewDataDictionary。 就快到了,只需这样调用即可:

Html.RenderPartial(
      "ProductImageForm", 
       image, 
       new ViewDataDictionary { { "index", index } }
); 

请注意,这将覆盖所有其他视图默认具有的默认 ViewData。 如果您要向 ViewData 添加任何内容,则它不会位于您传递给部分视图的新字典中。

RenderPartial takes another parameter that is simply a ViewDataDictionary. You're almost there, just call it like this:

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文