ASP.NET MVC + jqGrid嵌入式链接最佳实践

时间:2022-11-30 16:55:49

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

我有一个Mvc应用程序,我用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.

问题是我的一个列中有操作链接以对行执行某些操作。我在我的XML中使用CDATA标签重复这些;但问题是现在这个标签是在一个动作方法中生成的,所以所有的HTML都在我的控制器中(在TagBuilder中),这显然不是很好。

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.

我打算查看RenderPartialExtensions,但是你需要一个HtmlHelper实例,而且我正在使用brail视图引擎,所以我甚至不确定它是否会起作用。

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.

别人怎么处理这个?我想我可以创建一个视图,将实际的xml呈现为html,但之后我需要为每个xml数据源创建一个视图,我已经拥有了这些视图。

Thank you in advance for your input.

提前感谢您的意见。

2 个解决方案

#1


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.

我不确定你究竟在问什么,但我有一些XML数据 - 用于弃权,发行等的法律术语 - 我在我的观点中呈现。我使用了HtmlHelper扩展路由并创建了一个扩展,它接受文档的URL和将其转换为HTML的XSLT样式表。我使用LINQ XML类在MemoryStream中加载和呈现XML到HTML。然后它将此作为字符串返回,该字符串通过视图写入响应。如果您无权访问HTML帮助程序,则可以在静态类(无论是扩展程序是什么)上编写类似静态方法的内容,但不使用扩展语法。这可以在您喜欢的任何视图中呈现。

Example of my code:

我的代码示例:

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

#2


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. VB XML De / Serialization库

  4. ASP.NET MVC Project
  5. ASP.NET MVC项目

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:

VB XML程序集处理序列化仅处理业务(#1)模型的业务对象。所以我会输出这样的XElement:

<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:

问题出现了我正在使用jqGrid,我需要为每个发票添加元素到某些操作,所以我需要这样的东西:

<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:

我得到它的工作,但我不得不在控制器中生成所有的actionHtml,这显然不是一个很好的分离关注,我丢失了所有的HtmlHelper方法等。所以我最终以“正确”的方式解决了这个问题:

  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. 使用WebForms视图(我为我的页面使用brail视图)[所以我可以使用ContentType页面属性将其设置为XML]。

  3. Pass the collection of my raw business objects as the model data.
  4. 将原始业务对象的集合作为模型数据传递。

  5. Loop the model collection, building the actonHtml element for each invoice in the view (hence getting my HtmlHelper methods and such)
  6. 循环模型集合,为视图中的每个发票构建actonHtml元素(因此获取我的HtmlHelper方法等)

  7. 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.
  8. 要输出实际的业务元素,我可以简单地调用我的VB XML库并执行:invoice.ToXml()。Elements()。ToString(),我很好。

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.

为了进一步使这更加可重用,我创建了一个jqGrid母版页,它具有所有记录计数等内容区域,因此我可以将这些信息传递到实际页面上,只是担心输出元素数据。

#1


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.

我不确定你究竟在问什么,但我有一些XML数据 - 用于弃权,发行等的法律术语 - 我在我的观点中呈现。我使用了HtmlHelper扩展路由并创建了一个扩展,它接受文档的URL和将其转换为HTML的XSLT样式表。我使用LINQ XML类在MemoryStream中加载和呈现XML到HTML。然后它将此作为字符串返回,该字符串通过视图写入响应。如果您无权访问HTML帮助程序,则可以在静态类(无论是扩展程序是什么)上编写类似静态方法的内容,但不使用扩展语法。这可以在您喜欢的任何视图中呈现。

Example of my code:

我的代码示例:

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

#2


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. VB XML De / Serialization库

  4. ASP.NET MVC Project
  5. ASP.NET MVC项目

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:

VB XML程序集处理序列化仅处理业务(#1)模型的业务对象。所以我会输出这样的XElement:

<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:

问题出现了我正在使用jqGrid,我需要为每个发票添加元素到某些操作,所以我需要这样的东西:

<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:

我得到它的工作,但我不得不在控制器中生成所有的actionHtml,这显然不是一个很好的分离关注,我丢失了所有的HtmlHelper方法等。所以我最终以“正确”的方式解决了这个问题:

  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. 使用WebForms视图(我为我的页面使用brail视图)[所以我可以使用ContentType页面属性将其设置为XML]。

  3. Pass the collection of my raw business objects as the model data.
  4. 将原始业务对象的集合作为模型数据传递。

  5. Loop the model collection, building the actonHtml element for each invoice in the view (hence getting my HtmlHelper methods and such)
  6. 循环模型集合,为视图中的每个发票构建actonHtml元素(因此获取我的HtmlHelper方法等)

  7. 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.
  8. 要输出实际的业务元素,我可以简单地调用我的VB XML库并执行:invoice.ToXml()。Elements()。ToString(),我很好。

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.

为了进一步使这更加可重用,我创建了一个jqGrid母版页,它具有所有记录计数等内容区域,因此我可以将这些信息传递到实际页面上,只是担心输出元素数据。