【问题标题】:Controller method is not hitting in Datatables C# MVC控制器方法在 Datatables C# MVC 中没有命中
【发布时间】:2012-12-30 10:29:21
【问题描述】:

我在我的应用程序中使用 Datatables,但未触发获取数据控制器方法。 我可以在 UI 上渲染表格,但即将到来的数据为 NULL。

这是我的代码

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" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>http://stackoverflow.com/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 文件的外观

var rptTabs = function () {
    return {
        Init: function () {



            var placeholder = $("#rpt-tab");
            placeholder.setTemplateURL("/Templates/Home/report.htm");


            placeholder.load("/Templates/Home/report.htm");



        }
    }
} ();

我的 Home 控制器方法如下所示

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);
        }

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

【问题讨论】:

  • 这个rptTabs 变量是什么?它与您的问题有什么关系?
  • rpttabs 是一个选项卡,我将在其中呈现数据。这个第一个控件会来,然后它会加载 HTML 模板。从那里数据表脚本触发器
  • 我不确定我是否理解。你能显示正在使用它的代码吗?
  • HI Darin 问题已解决。我刚刚更改了 sAjaxSource 的语法,例如“sAjaxSource”:'/Home/GridData',它开始工作了..非常感谢您的支持..
  • 哦,您使用的是 WebForms 视图引擎而不是 Razor?

标签: c# jquery asp.net-mvc datagrid grid


【解决方案1】:

问题与您在静态 HTML 模板中使用服务器端帮助程序 (Url.Action("GridData", "Home")) 的事实有关,因为您错误地复制粘贴了 my solution from here 而不使其适应您的场景。此外,您使用的是 WebForms 视图引擎,而不是 Razor。

因此,我建议您将此模板设置为 ASPX WebForm,通过控制器操作提供服务,这将允许您在内部使用服务器端助手。

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

然后你就会有一个对应的视图:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>https://stackoverflow.com/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>

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-17
    • 1970-01-01
    • 2019-11-13
    • 1970-01-01
    • 2015-03-03
    相关资源
    最近更新 更多