views:

967

answers:

2

I have a Mvc app that I am replacing some hard rendered tables with jqGrid [XML].

Problem is I have action links in one of my columns to perform certain actions on the rows. I have these duplicated using CDATA tags in my XML; however the problem is that now this tag is generated in an action method so all the HTML is in my controller (in a TagBuilder) and this is decidedly NOT good.

I was going to look into the RenderPartialExtensions but you need an HtmlHelper instance for that, plus I am using the brail view engine so I am not even sure that would work.

How have other people handled this?
I guess I could create a view that renders the actual xml like html but then I would need to create a view for each xml data source and I already have those.

Thank you in advance for your input.

A: 

I'm not sure exactly what you're asking but I have some XML data -- legalese for waivers, releases, etc -- that I render in my views. I went the HtmlHelper extension route and created an extension that takes the URL of the document and the XSLT stylesheet that translates it to HTML. I use the LINQ XML classes to load and render the XML to HTML in a MemoryStream. It then returns this as a string which is written to the response via the view. If you don't have access to the HTML helper, you could write something similar as a static method on a static class (which is what the extension is anyway) but not using the extension syntax. This could be rendered in any view that you like.

Example of my code:

 <%= Html.RenderXML( Url.Content( "~/App_Data/waiver.xml" ),
                     Url.Content( "~/Content/styles/waiver.xsl" ) ) %>
tvanfosson
A: 

To clarify more of the architecture and how I ended up solving it. I have the following projects / assemblies:

  1. Business Objects
  2. VB XML De/Serialization library
  3. ASP.NET MVC Project

The VB XML assembly handles serializing the business objects dealing with only the business (#1) model. So i would output an XElement something like this:

<invoice>
   <invoiceId>1234</invoiceId>
   <customer>Hudsucker Industries</customer>
   <otherBusinessData>etc, etc</otherBusinessData>
</invoice>

The problem arises that I am using jqGrid and I need to add elements to each invoice to certain actions so I need something like:

<invoice>
   <actionHtml><![CDATA[   ...some HTML such as links... ]]></actionHtml>
   <invoiceId>1234</invoiceId>
   <customer>Hudsucker Industries</customer>
   <otherBusinessData>etc, etc</otherBusinessData>
</invoice>

I got it to work but I had to generate all the actionHtml in the controller which is obviously not a good separation of concerns and I lost all my HtmlHelper methods and such. So how I ended up solving it the 'correct' way is:

  1. Using a WebForms view (I use brail views for my pages) [so I can use the ContentType page attribute to set it to XML].
  2. Pass the collection of my raw business objects as the model data.
  3. Loop the model collection, building the actonHtml element for each invoice in the view (hence getting my HtmlHelper methods and such)
  4. To output the actual business elements I can simply invoke my VB XML library and do a: invoice.ToXml().Elements().ToString() and I am good.

To further make this more reusable I created a jqGrid master page that has all the record counts and such in content areas so I can just pass that information in on the actual pages and just worry about outputting the element data.

Andrew Burns