如何使用Html.RenderPartial()将任意数据传递给ASP.NET MVC中的用户控件? [英] How do you pass an arbitrary bit of data to a user control in ASP.NET MVC using Html.RenderPartial()?
问题描述
我有一个强类型的用户控件(部分"),我希望能够从其包含的视图中传递一些其他信息.例如,我有绑定到产品类的视图,并且我有一个部分也被强类型化为同一模型,但是我还需要将imageSize的附加参数传递给我的部分.我希望能够做这样的事情:
I have a strongly typed user control ("partial") and I'd like to be able to pass it some additional information from its containing view. For example, I have view that's bound to a product class and i have a partial that also is strongly typed to that same model, but I also need to pass an additional parameter for imageSize to my partial. I'd like to be able to do something like this:
<% Html.RenderPartial("_ProductImage", ViewData.Model, new { imageSize = 100 }); %>
据我所知没有办法,但是我希望比我聪明的人可以找到解决方案;)
As far as I know there is no way to do this, but I'm hoping that someone smarter than me may have a solution ;)
推荐答案
更改局部模型的类型:
class PartialModel
{
public int ImageSize { get; set; }
public ParentModelType ParentModel { get; set; }
}
现在通过它:
<% Html.RenderPartial("_ProductImage",
new PartialModel() { ImageSize = 100, ParentModel = ViewData.Model }); %>
这篇关于如何使用Html.RenderPartial()将任意数据传递给ASP.NET MVC中的用户控件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!