2016-09-09 7 views
2

コントローラでJSONリストを返そうとしています。どこが間違っているのか分かりません。どんな助けでも大歓迎です。私はオンラインで発見されたグリッドを実装しようとしています。ここでMVCコントローラがJSONリストを返す

は私のコントローラである:ここでは

//[HttpPost] 
     public JsonResult JqueryTest() 

     { 
      var estimateDetails = db.EstimateDetail 
       .Include(x => x.EstimationItem) 
       .Include(x => x.EstimateHeader); 

      return Json(estimateDetails, JsonRequestBehavior.AllowGet); 

     } 

が私の見解です:stephen.vakil状態として、第1

@model IEnumerable <EstimationTools.Models.Entities.EstimateDetail> 



    ViewBag.Title = "JqueryTest"; 
} 

<h2>JqueryTest</h2> 

<html lang="en"> 
<head> 
    <!-- The jQuery library is a prerequisite for all jqSuite products --> 
    <script type="text/ecmascript" src="../../../js/jquery.min.js"></script> 
    <!-- We support more than 40 localizations --> 
    <script type="text/ecmascript" src="../../../js/trirand/i18n/grid.locale-en.js"></script> 
    <!-- This is the Javascript file of jqGrid --> 
    <script type="text/ecmascript" src="../../../js/trirand/jquery.jqGrid.min.js"></script> 
    <!-- This is the localization file of the grid controlling messages, labels, etc. 
    <!-- A link to a jQuery UI ThemeRoller theme, more than 22 built-in and many more custom --> 
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap.min.css"> 
    <!-- The link to the CSS that the grid needs --> 
    <link rel="stylesheet" type="text/css" media="screen" href="../../../css/trirand/ui.jqgrid-bootstrap.css" /> 


    <script> 
     $.jgrid.defaults.width = 780; 
     $.jgrid.defaults.styleUI = 'Bootstrap'; 
    </script> 
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/js/bootstrap.min.js"></script> 
    <meta charset="utf-8" /> 
    <title>jqGrid Loading Data - JSON</title> 
</head> 
<body> 
    <div style="margin-left:20px"> 
     <table id="jqGrid"></table> 
     <div id="jqGridPager"></div> 
    </div> 
    <script type="text/javascript"> 



     $(document).ready(function() { 

      $("#jqGrid").jqGrid({ 
       url: 'EstimationTool/plugins/jqGrid', 
       datatype: "json", 
       colModel: [ 
        { label: 'Estimate', name: 'Estimate', width: 75 }, 
        { label: 'Contingency', name: 'Contingency', width: 90 }, 
        { label: 'Comment', name: 'Comment', width: 100 }, 
        { label: 'Subactivity', name: 'EstimationItem.Subactivity', width: 100 }, 
        { label: 'EstimationArea', name: 'EstimationItem.Area.EstimationArea', width: 100 }, 
        { label: 'Description', name: 'EstimationItem.GeneralActivity.Description', width: 100 } 
        // sorttype is used only if the data is loaded locally or loadonce is set to true 
       ], 
       viewrecords: true, // show the current page, data rang and total records on the toolbar 
       width: 780, 
       height: 200, 
       rowNum: 30, 
       loadonce: true, // this is just for the demo 
       pager: "#jqGridPager" 
      }); 
     }); 

    </script> 

</body> 
</html> 
+0

ヨーヨーは、JSONデータが来ることを期待していますか?期待される行動は何ですか?現在何が起こっていますか? – Shyju

+1

あなたの 'url'はあなたのコントローラのURLと一致していないようです。 –

答えて

2

まず最初に...、あなたのURLに誤りがあなたのように、そこにあります

url: 'EstimationTool/plugins/jqGrid' 

通常の構文は次のとおりです:ので、 "jqGrid" というアクションを指している'{Controller}/{Action}/'

あなたのケースでは、アクション名は上記のように"JqueryTest"です。だから、私はあなたのコントローラの名前を知らないが、私はあなたがその考えを持っていることを願っています。このような何か:一方

url: 'YourController/JqueryTest' 

は、あなたの行動の中に、あなたの代わりにリストを返す必要があります...そう、ちょうどクエリの最後に.ToList()を追加したり、パラメータに追加します:

return Json(estimateDetails.**ToList()**, JsonRequestBehavior.AllowGet); 

よろしく、

+2

re: "代わりにリストを返す"。これは重要。 EF DbSetのように見えるものを返すと、例外がスローされます(コンテキストは、アクセスしようとする前に破棄されます)。 ToListを使用してデータを具体化する必要があります。 –

関連する問題