ASP.NET MVC dynamically load user control, like in

2019-06-13 06:27发布

问题:

Hallo, my team and I are about to start a project, and we would really like to use ASP.NET MVC, not the Web Forms. Why? Nothing special, just we want to learn it well and to measure what would be better to use in future projects.

But, our project has important request. We need ability to dynamically load user controls, that can be created and uploaded in runtime. With ASP.NET Web Forms and it's LoadControl method it's simple.

Can we do something like that with MVC?

Tnx

回答1:

Partial views are what you want.

MVC - Dynamically loading Partial Views



回答2:

partial views will do the trick.. or you might want to rethink why you are using a user control. is this a third party control? could this be rewritten?



回答3:

You can use

<%

foreach (var thing in things)

    Html.RenderPartial("location/page.ascx",thing);

%>

from within your view