【问题标题】:I want to show the json data from action in the datatable我想在数据表中显示来自动作的 json 数据
【发布时间】:2021-03-24 22:03:46
【问题描述】:

桌子是空的。没有数据。

public JsonResult GetData(){
Uri url=new Uri("https://rallycoding.herokuapp.com/api/music_albums");
WebClient client=new WebClient();
client.Encoding=System.Text.Encoding.UTF8;
string json=client.DownloadString(url);
return Json(json);
}
<table id="example" class="table table-bordered table-striped table-sm">
     <thead>
         <tr>
            <th>title</th>
            <th>artist</th>
            <th>url</th>

         </tr>
     </thead>
</table>
<script>
 $(document).ready(function(){
$.ajax({
datatype:"json",
url:"/Home/GetData",
succes:function(data){
    var obj=Json.parse(data);
}
})
var table=$('#example').DataTable({
'data':obj,
'paging':true,
'searching':true,
'ordering':true
})
})
</script>

我可以将 JSON 数据解析到列表中并使用 viewbag 将其写入表中,但我必须将其 JSON 到数据表中。

你能帮帮我吗?

【问题讨论】:

    标签: asp.net json datatable


    【解决方案1】:

    您需要在绑定操作中添加列,这里是代码

      $(document).ready(function() {
      
         $.get("/Home/GetData", function(data, status){
         var obj=JSON.parse(data);
               var table=$('#example').DataTable({
            'data':obj,
            'paging':true,
            'searching':true,
            'ordering':true,
         "columns": [
            { data: "title" },
            { data: "artist" },
            { data: "url" }
            ]
            });
        });
      });
     <!--Import jQuery before export.js-->
        <script type="text/javascript" src="https://code.jquery.com/jquery-2.1.1.min.js"></script>
    
    
        <!--Data Table-->
        <script type="text/javascript"  src=" https://cdn.datatables.net/1.10.13/js/jquery.dataTables.min.js"></script>
        <script type="text/javascript"  src=" https://cdn.datatables.net/buttons/1.2.4/js/dataTables.buttons.min.js"></script>
    
        <!--Export table buttons-->
        <script type="text/javascript"  src="https://cdnjs.cloudflare.com/ajax/libs/jszip/2.5.0/jszip.min.js"></script>
        <script type="text/javascript" src="https://cdn.rawgit.com/bpampuch/pdfmake/0.1.24/build/pdfmake.min.js" ></script>
        <script type="text/javascript"  src="https://cdn.rawgit.com/bpampuch/pdfmake/0.1.24/build/vfs_fonts.js"></script>
        <script type="text/javascript" src="https://cdn.datatables.net/buttons/1.2.4/js/buttons.html5.min.js"></script>
        <script type="text/javascript" src="https://cdn.datatables.net/buttons/1.2.1/js/buttons.print.min.js"></script>
    
    
    
    <table id="example" class="table table-bordered table-striped table-sm">
         <thead>
             <tr>
                <th>title</th>
                <th>artist</th>
                <th>url</th>
    
             </tr>
         </thead>
    </table>

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-10-30
      • 1970-01-01
      相关资源
      最近更新 更多