views:

18

answers:

2

Hi,

I have a fairly complex object which has some C# code written to render it as HTML in various views.

There is also a view which can call an AJAX method of a controller, which returns the complex object serialized to JSON which should then be displayed.

This seems to leave me requriring complicated duplicate code to render the resulting JSON as HTML using Javascript/jQuery.

The obvious solution is to render the HTML in the controller action and return this from the AJAX call. However this seems in violation of the MVC pattern so not really a good option.

Is there a different way I can render the object returned from the AJAX method making use of the existing C# code?

Thanks.

+2  A: 

Create a PartialView to which you render the object, and return that.

Tomas Lycken
So simple... not sure why I couldn't think of this but it's exactly what I was looking for. Got myself stuck in a JSON rut for some reason! Thanks very much.
TonE
A: 

Hey,

As mentioned, either create a PartialView user control and return that, which you can inject the HTML in nicely (returns the HTML as a string) or you can use a templating option in JQuery or something else to do the UI generation for you.

HTH.

Brian
Thanks for your comment. PartialView was exactly what I was looking for. Will look into jQuery templating in future.
TonE