views:

139

answers:

1

Hey Guys,

I've read the related questions but I cannot find one that suits my problem(or i'm just stupid).

Basically i have a factory that renders a certain "template" and that template contains certain "entities". These entities can render themselves and have a void Render method that constructs the HTML and returns it to the template factory.

Now, for maintainability this is a nightmare. If the customer wants a space in between the html the dlls would need to be recompiled (Which is terrible!)

I need a way to move the logic + HTML out of the void Render method and into some kind of control, parse the control and return HTML at run time. So that the code can be relatively well maintained.

I'm using Asp.Net MVC and the template factory is a class library. Is there any way to accomplish this?

Thanks!

+1  A: 

Sounds like what you want is a strongly-typed ViewUserControl for each "entity." This is basically how I've implemented widgets in a project I'm working on. Each widget has it's own widget model and a partial view (ViewUserControl) strongly-typed to that model. The widget uses the model's properties to render a view of that widget.

tvanfosson
That is exactly what i want to accomplish! Thanks! Now the question remains, how do i do this. Must the factory then just return a list of control names + instantiated models and the view just loop through each item and render them - Html.RenderPartial("EntityName",EntityModel)?
The_Butcher
That's basically how I am doing it, though I place all of the models in an enumerable inside the page model and have a method on my base controller that uses a factory to create all the widgets for that page and put them into this master model. On the page I simply iterate through the enumerable and have it call RenderPartial with the widget model's idea of which partial to render: foreach (var widget in Model.Widgets) { Html.RenderPartial( widget.ViewName, widget.Model ); }
tvanfosson
Thanks a lot! I will implement this!
The_Butcher