0

アプリケーションで 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");



        }
    }
} ();

これが私のホームコントローラーメソッドの外観です

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

私の実装で何が問題なのか教えてください。

4

1 に答える 1

3

この問題は、静的 HTML テンプレート内でサーバー側ヘルパー ( ) を使用しているという事実に関連しています。これは、シナリオに適合させずにUrl.Action("GridData", "Home")誤ってコピー アンド ペーストしたためです。my solution from hereさらに、Razor ではなく WebForms ビュー エンジンを使用しています。

したがって、このテンプレートを 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>

次に、テンプレートをロードするときに、このコントローラーへの正しいパスを指定します (サーバー側ヘルパーを使用してもう一度)。

于 2013-01-16T07:15:15.030 に答える