将 ViewData 传递给 RenderPartial

发布于 2024-08-19 04:57:34 字数 464 浏览 6 评论 0原文

我正在尝试调用此方法:

RenderPartialExtensions.RenderPartial Method (HtmlHelper, String, Object, ViewDataDictionary)

http://msdn.microsoft.com/ en-us/library/dd470561.aspx

但我没有看到任何在表达式中构造 ViewDataDictionary 的方法,例如:

<% Html.RenderPartial("BlogPost", Post, new { ForPrinting = True }) %>

有什么想法如何做到这一点吗?

I'm trying to call this method:

RenderPartialExtensions.RenderPartial Method (HtmlHelper, String, Object, ViewDataDictionary)

http://msdn.microsoft.com/en-us/library/dd470561.aspx

but I don't see any way to construct a ViewDataDictionary in an expression, like:

<% Html.RenderPartial("BlogPost", Post, new { ForPrinting = True }) %>

Any ideas how to do that?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(4

破晓 2024-08-26 04:57:34

这对我有用:

<% Html.RenderPartial("BlogPost", Model, new ViewDataDictionary{ {"ForPrinting", "true"} });%>

This worked for me:

<% Html.RenderPartial("BlogPost", Model, new ViewDataDictionary{ {"ForPrinting", "true"} });%>
尽揽少女心 2024-08-26 04:57:34

我已经设法使用以下扩展方法来做到这一点:

public static void RenderPartialWithData(this HtmlHelper htmlHelper, string partialViewName, object model, object viewData) {
  var viewDataDictionary = new ViewDataDictionary();
  if (viewData != null) {
    foreach (PropertyDescriptor prop in TypeDescriptor.GetProperties(viewData)) {
      object val = prop.GetValue(viewData);
      viewDataDictionary[prop.Name] = val;
    }
  }
  htmlHelper.RenderPartial(partialViewName, model, viewDataDictionary);
}

这样调用它:

<% Html.RenderPartialWithData("BlogPost", Post, new { ForPrinting = True }) %>

I've managed to do this with the following extension method:

public static void RenderPartialWithData(this HtmlHelper htmlHelper, string partialViewName, object model, object viewData) {
  var viewDataDictionary = new ViewDataDictionary();
  if (viewData != null) {
    foreach (PropertyDescriptor prop in TypeDescriptor.GetProperties(viewData)) {
      object val = prop.GetValue(viewData);
      viewDataDictionary[prop.Name] = val;
    }
  }
  htmlHelper.RenderPartial(partialViewName, model, viewDataDictionary);
}

calling it this way:

<% Html.RenderPartialWithData("BlogPost", Post, new { ForPrinting = True }) %>
红墙和绿瓦 2024-08-26 04:57:34

您可以这样做:

new ViewDataDictionary(new { ForPrinting = True })

因为 viewdatadictionary 可以在其构造函数中获取一个对象来反映。

You can do:

new ViewDataDictionary(new { ForPrinting = True })

As viewdatadictionary can take an object to reflect against in its constructor.

梦幻之岛 2024-08-26 04:57:34

这并不完全是您所要求的,但您可以使用 ViewContext.ViewBag。

// in the view add to the ViewBag:
ViewBag.SomeProperty = true;
...
Html.RenderPartial("~/Views/Shared/View1.cshtml");

// in partial view View1.cshtml then access the property via ViewContext:
@{
    bool someProperty = ViewContext.ViewBag.SomeProperty;
}

This is not exactly what you asked for, but you can use ViewContext.ViewBag.

// in the view add to the ViewBag:
ViewBag.SomeProperty = true;
...
Html.RenderPartial("~/Views/Shared/View1.cshtml");

// in partial view View1.cshtml then access the property via ViewContext:
@{
    bool someProperty = ViewContext.ViewBag.SomeProperty;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文