本文介绍了传递额外的ViewData到一个强类型管窥的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个强类型的局部视图,需要一个ProductImage,当它被渲染我也想为它提供一些额外的ViewData,我在包含页面动态创建。我怎么能同时通过我的强类型的对象,我自定义的ViewData的局部视图与调用的RenderPartial?

  VAR指数= 0;
的foreach(在Model.Images.OrderBy VAR图像(P => p.Order))
{
  Html.RenderPartial(ProductImageForm,图像); //<通指数偏
  指数++;
}


解决方案

的RenderPartial另需参数,它是一个简单的的ViewDataDictionary。你几乎没有,只是把它是这样的:

  Html.RenderPartial(
      ProductImageForm
       图片,
       新的ViewDataDictionary {{指数,指数}}
);

请注意,这将覆盖默认的ViewData你所有的其他意见都默认。如果要添加任何ViewData的,它不会在你传递到你的局部视图这个新的字典。

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++;
}
解决方案

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.

这篇关于传递额外的ViewData到一个强类型管窥的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-14 19:22