如何使用Html.RenderPartial()将任意位数据传递给ASP.NET MVC中的用户控件?

时间:2022-08-25 23:23:33

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:

我有一个强类型的用户控件(“部分”),我希望能够从其包含的视图中传递一些其他信息。例如,我有一个视图绑定到一个产品类,我有一个部分也强烈键入相同的模型,但我还需要将imageSize的附加参数传递给我的部分。我希望能够做到这样的事情:

<% 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 ;)

据我所知,没有办法做到这一点,但我希望比我聪明的人可以有解决方案;)

3 个解决方案

#1


5  

Change the type of the partial model:

更改部分模型的类型:

class PartialModel 
{
    public int ImageSize { get; set; }
    public ParentModelType ParentModel { get; set; }
}

Now pass it:

现在通过它:

<% Html.RenderPartial("_ProductImage", 
    new PartialModel() { ImageSize = 100, ParentModel = ViewData.Model }); %>

#2


2  

Not the most beautiful solution

不是最美丽的解决方案

<% ViewData["imageSize"] = 100; %>

<% Html.RenderPartial("_ProductImage"); %>

the ViewData is passed by default

默认情况下传递ViewData

#3


1  

I use a generic class model - which is similar in concept to the approach suggested by Craig.

我使用泛型类模型 - 其概念与Craig建议的方法类似。

I kind of wish MS would create an overload to RenderPartial to give us the same functionality. Just an additional object data parameter would be fine.

我希望MS能够为RenderPartial创建一个重载,以便为我们提供相同的功能。只需一个额外的对象数据参数就可以了。

Anyway, my approach is to create a PartialModel which uses generics so it can be used for all .ascx controls.

无论如何,我的方法是创建一个使用泛型的PartialModel,以便它可以用于所有.ascx控件。

 public class PartialControlModel<T> : ModelBase 
    {
        public T ParentModel { get; set; }
        public object Data { get; set; }

        public PartialControlModel(T parentModel, object data) : base()
        {
            ParentModel = parentModel;
            Data = data;
        }
    }

The .ascx control should inherit from the correct PartialControlModel if you want the view to be strongly typed, which most likely you do if you've got this far.

如果您希望视图是强类型的,那么.ascx控件应该从正确的PartialControlModel继承,如果你已经这么做了很可能你会这样做。

public partial class ThumbnailPanel : 
                     ViewUserControl<PartialControlModel<GalleryModel>>

Then you render it like this :

然后你像这样渲染它:

<% Html.RenderPartial("ThumbnailPanel", 
new PartialControlModel<GalleryModel>(ViewData.Model, tag)); %>

Of course when you refer to any parent model items you must use this syntax :

当然,当您引用任何父模型项时,您必须使用以下语法:

ViewData.Model.ParentModel.Images

You can get the data and cast it to the correct type with :

您可以获取数据并将其转换为正确的类型:

ViewData.Model.Data

If anyone has a suggestion on how to improve the generics I'm using please let me know.

如果有人建议如何改进我正在使用的仿制药,请告诉我。

#1


5  

Change the type of the partial model:

更改部分模型的类型:

class PartialModel 
{
    public int ImageSize { get; set; }
    public ParentModelType ParentModel { get; set; }
}

Now pass it:

现在通过它:

<% Html.RenderPartial("_ProductImage", 
    new PartialModel() { ImageSize = 100, ParentModel = ViewData.Model }); %>

#2


2  

Not the most beautiful solution

不是最美丽的解决方案

<% ViewData["imageSize"] = 100; %>

<% Html.RenderPartial("_ProductImage"); %>

the ViewData is passed by default

默认情况下传递ViewData

#3


1  

I use a generic class model - which is similar in concept to the approach suggested by Craig.

我使用泛型类模型 - 其概念与Craig建议的方法类似。

I kind of wish MS would create an overload to RenderPartial to give us the same functionality. Just an additional object data parameter would be fine.

我希望MS能够为RenderPartial创建一个重载,以便为我们提供相同的功能。只需一个额外的对象数据参数就可以了。

Anyway, my approach is to create a PartialModel which uses generics so it can be used for all .ascx controls.

无论如何,我的方法是创建一个使用泛型的PartialModel,以便它可以用于所有.ascx控件。

 public class PartialControlModel<T> : ModelBase 
    {
        public T ParentModel { get; set; }
        public object Data { get; set; }

        public PartialControlModel(T parentModel, object data) : base()
        {
            ParentModel = parentModel;
            Data = data;
        }
    }

The .ascx control should inherit from the correct PartialControlModel if you want the view to be strongly typed, which most likely you do if you've got this far.

如果您希望视图是强类型的,那么.ascx控件应该从正确的PartialControlModel继承,如果你已经这么做了很可能你会这样做。

public partial class ThumbnailPanel : 
                     ViewUserControl<PartialControlModel<GalleryModel>>

Then you render it like this :

然后你像这样渲染它:

<% Html.RenderPartial("ThumbnailPanel", 
new PartialControlModel<GalleryModel>(ViewData.Model, tag)); %>

Of course when you refer to any parent model items you must use this syntax :

当然,当您引用任何父模型项时,您必须使用以下语法:

ViewData.Model.ParentModel.Images

You can get the data and cast it to the correct type with :

您可以获取数据并将其转换为正确的类型:

ViewData.Model.Data

If anyone has a suggestion on how to improve the generics I'm using please let me know.

如果有人建议如何改进我正在使用的仿制药,请告诉我。