控制器方法不在Datatables C#MVC中

编程入门 行业动态 更新时间:2024-10-11 19:17:16
本文介绍了控制器方法不在Datatables C#MVC中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我在我的应用程序中使用Datatables,但是获取数据控制器方法没有被触发。 我可以在用户界面上呈现表格,但是数据是否为NULL。

I am using Datatables in my application, but to fetch the data controller method is not getting triggered. I am able to render the table on UI, but the data is coming is NULL.

这是我的代码

SITE.MASTER中导入的项目

Imported items in SITE.MASTER

<link href="/Scripts/DataTables/media/css/demo_page.css" type="text/css" rel="stylesheet" /> <link href="/Scripts/DataTables/media/css/demo_table.css" type="text/css" rel="stylesheet" /> <script src="/Scripts/Lib/jquery-1.4.2.js" type="text/javascript" language="javascript"></script> <script type="text/javascript" charset="utf-8" src="/Scripts/DataTables/media/js/jquery.dataTables.js"></script>

这是我的HTML外观

!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "www.w3/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="www.w3/1999/xhtml"> <head> <title>stackoverflow/questions/6946559/jqgrid-please-help</title> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <script type="text/javascript" charset="utf-8"> $(document).ready(function () { $('#example').dataTable({ bProcessing: true, sAjaxSource: '@Url.Action("GridData", "Home")' }); }); </script> </head> <div id="dynamic"> <table cellpadding="0" cellspacing="0" border="0" class="display" id="example"> <thead> <tr> <th width="20%">Rendering engine</th> <th width="25%">Browser</th> <th width="25%">Platform(s)</th> <th width="15%">Engine version</th> <th width="15%">CSS grade</th> </tr> </thead> <tbody> </tbody> </table> </div> </html>

这里我如何加载HTML的JS文件看起来

Here how my JS file which loads HTML looks

var rptTabs = function () { return { Init: function () { var placeholder = $("#rpt-tab"); placeholder.setTemplateURL("/Templates/Home/report.htm"); placeholder.load("/Templates/Home/report.htm"); } } } ();

这里我的家庭控制器方法如何看起来

Here how my Home controller method looks

public ActionResult GridData() { return Json(new { aaData = new[] { new [] { "Trident", "Internet Explorer 4.0", "Win 95+", "4", "X" }, new [] { "Gecko", "Firefox 1.5", "Win 98+ / OSX.2+", "1.8", "A" }, new [] { "Webkit", "iPod Touch / iPhone", "iPod", "420.1", "A" } } }, JsonRequestBehavior.AllowGet); }

请告诉我我的实现有什么问题。

Please tell me what is wrong with my implementation.

推荐答案

该问题与您正在使用服务器端帮助器( Url.Action(GridData,首页))内部的静态HTML模板,因为您错误地复制粘贴 我的解决方案来自这里 ,而不适应您的方案。此外,您使用的是WebForms视图引擎,而不是Razor。

The problem is related to the fact that you are using a server side helper (Url.Action("GridData", "Home")) inside a static HTML template since you have incorrectly copy-pasted my solution from here without adapting it to your scenario. Furthermore you are using the WebForms view engine and not Razor.

所以我建议您将此模板作为ASPX WebForm,通过控制器操作提供,这将允许您使用

So I would recommend you making this template an ASPX WebForm, served through a controller action which would allow you to use server side helpers inside.

public class TemplatesController: Controller { public ActionResult Report() { return View(); } }

然后你将有一个相应的视图: p>

And then you will have a corresponding view:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "www.w3/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="www.w3/1999/xhtml"> <head> <title>stackoverflow/questions/6946559/jqgrid-please-help</title> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <script type="text/javascript" charset="utf-8"> $(document).ready(function () { $('#example').dataTable({ bProcessing: true, sAjaxSource: '<%= Url.Action("GridData", "Home") %>' }); }); </script> </head> <div id="dynamic"> <table cellpadding="0" cellspacing="0" border="0" class="display" id="example"> <thead> <tr> <th width="20%">Rendering engine</th> <th width="25%">Browser</th> <th width="25%">Platform(s)</th> <th width="15%">Engine version</th> <th width="15%">CSS grade</th> </tr> </thead> <tbody> </tbody> </table> </div> </html>

然后在加载模板时指定此控制器的正确路径(再次使用服务器端帮助器)。

and then specify the correct path to this controller when loading the template (once again by using server side helper).

更多推荐

控制器方法不在Datatables C#MVC中

本文发布于:2023-11-24 19:58:58,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1626612.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:控制器   方法   MVC   Datatables

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!