将 jQuery DataTable 与 ASP.NET MVC 结合使用的正确 AJAX 模式是什么?
我有一个 jQuery DataTable 配置为针对 ASP.NET MVC 服务器进行服务器端处理。
为了实现渐进增强,将呈现 HTML 表,然后升级为 DataTable。当 DataTable 进行 AJAX 调用来检索更多数据时,它期望返回的 JSON 元素与现有的表列布局相关联。
这似乎会导致一些架构上的摩擦,因为:
在初始页面呈现期间:
- 控制器将数据库数据转换为 DTO 并建立 ViewData 模型。
- ViewPage借助HtmlHelper等将ViewData模型转换为HTML。AJAX
更新时:
- Controller将数据库数据转换为表格单元格数据,带有超链接等。DataTable
- 将返回的数据直接渲染到表格单元格中。
这里的要点是,控制器现在被迫了解表渲染布局,而它的责任应该仅限于传回 DTO。
这里的正确模式是什么?如何支持 AJAX 调用并遵守控制器中的单一职责原则?
一些清晰度:
我的 DTO 有三个属性:
public class AccountListing
{
public int Id { get; set; }
public string AccountCode { get; set; }
public string AccountName { get; set; }
}
呈现的 DataTable 有四列:
+--------------+--------------+------+------+
| Account Code | Account Name | View | Edit |
+--------------+--------------+------+------+
| 12345 | FooBar Inc. | Link | Link |
+--------------+--------------+------+------+
ViewPage 将 3 属性 DTO 呈现为 4 列表。 如果控制器传回与 AJAX JSON 相同的 3 个属性 DTO,则数据表会抱怨缺少列...数据表期望返回的 JSON 表示单元格元素,而不是源数据。
目前我正在使用 jQuery 数组转换和自定义表格渲染来解决这个问题,这有效地复制了 ViewPage 的逻辑。
还有其他选择吗?
I have a jQuery DataTable configured for server-side processing against an ASP.NET MVC server.
To achieve progressive enhancement, an HTML table is rendered, and then upgraded to a DataTable. When the DataTable makes an AJAX call to retrieve more data, it expects elements of the returned JSON to correlate with the existing table-column layout.
This seems to cause some architectural friction, because:
During initial page rendering:
- The Controller transforms database data into DTOs and establishes a ViewData model.
- The ViewPage transforms the ViewData model into HTML with the help of HtmlHelper, etc.
Duriung AJAX updates:
- The Controller transforms database data into table-cell data, with hyperlinks, etc.
- The DataTable renders the returned data directly into the table cells.
The point here is that the Controller is now forced into knowing about the table rendering layout, when it's responsibility should be limited to passing back DTOs.
What's the correct pattern here? How do I support AJAX calls and adhere to the single-responsibility principal in the controller?
Some clarity:
My DTO has three properties:
public class AccountListing
{
public int Id { get; set; }
public string AccountCode { get; set; }
public string AccountName { get; set; }
}
The rendered DataTable has four columns:
+--------------+--------------+------+------+
| Account Code | Account Name | View | Edit |
+--------------+--------------+------+------+
| 12345 | FooBar Inc. | Link | Link |
+--------------+--------------+------+------+
The ViewPage renders the 3 property DTO into a 4 column table.
If the Controller passes back the same 3 property DTO as AJAX JSON, the data table complains about missing columns... the data table expects the returned JSON to represent cell elements, as opposed to source data.
Currently I'm solving this using jQuery array transforms and custom table rendering, which effectively duplicates the logic of the ViewPage.
Is there any alternative?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您当前的解决方案(在客户端进行渲染)对我来说听起来不错。
问题是,您的 DTO 不必与您的视图页面显示的内容相对应。流行的模式是针对这些情况使用 ViewModel 对象。
例如:
请参阅[viewmodel]、[模型-视图-viewmodel]。
Your current solution (doing the rendering on the client side) sound good to me.
The thing is, your DTOs do not have to correspond to what your view pages display. The popular pattern is having ViewModel objects for these situations.
For example :
See [viewmodel], [model-view-viewmodel].