2016-09-09 4 views
2

Ich versuche, eine JSON-Liste in meinem Controller zurückgeben. Ich bin mir nicht sicher, wo ich falsch liege. Jede Hilfe würde sehr geschätzt werden. Ich versuche ein online gefundenes Raster zu implementieren.MVC Controller Return JSON-Liste

Hier ist mein Controller:

@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

Wo erwarten yo die JSON-Daten zu kommen? Was ist dein erwartetes Verhalten? Was passiert gerade? – Shyju

+1

Ihre URL scheint nicht mit der URL Ihres Controllers übereinzustimmen. –

Antwort

2

Das erste, was zuerst ... als stephen.vakil Staaten gibt es einen Fehler in der URL, wie Sie:

//[HttpPost] 
     public JsonResult JqueryTest() 

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

      return Json(estimateDetails, JsonRequestBehavior.AllowGet); 

     } 

hier meiner Ansicht nach weisen so auf eine Aktion namens "jqGrid":

url: 'EstimationTool/plugins/jqGrid' 

Die übliche Syntax: '{Controller}/{Action}/'

In Ihrem Fall lautet der Aktionsname wie oben angegeben "JqueryTest". Also, ich kenne den Namen Ihres Controllers nicht, aber ich hoffe, Sie haben die Idee. Etwas wie folgt aus:

url: 'YourController/JqueryTest' 

Auf der anderen Seite, in Ihrer Aktion, sollten Sie eine Liste an Stelle zurückkehren ... so, fügen Sie einfach .ToList() am Ende der Abfrage oder hängen Sie ihn an Ihre Parameter:

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

Grüße,

+2

re: "gebe stattdessen eine Liste zurück". Das ist wichtig. Wenn Sie nur das zurückgeben, was wie ein EF DbSet aussieht, wird eine Ausnahme ausgelöst (der Kontext wird entsorgt, bevor versucht wird, darauf zuzugreifen). ToList muss verwendet werden, um die Daten zu materialisieren. –