Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/301.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# jqGrid在MVC中不显示json数据_C#_Jquery_Json_Asp.net Mvc_Jqgrid - Fatal编程技术网

C# jqGrid在MVC中不显示json数据

C# jqGrid在MVC中不显示json数据,c#,jquery,json,asp.net-mvc,jqgrid,C#,Jquery,Json,Asp.net Mvc,Jqgrid,jqGrid没有显示从MVC控制器获取的json数据,它还有一个emptyrecords标签,在本例中也不可见。这是我的jQuery代码- $(function () { $("#JQGrid1").jqGrid({ url: "/CertificateDetails/GetCertificateDetails", datatype: 'json', mtype: 'Get',

jqGrid没有显示从MVC控制器获取的json数据,它还有一个emptyrecords标签,在本例中也不可见。这是我的jQuery代码-

$(function () {
            $("#JQGrid1").jqGrid({
                url: "/CertificateDetails/GetCertificateDetails",
                datatype: 'json',
                mtype: 'Get',
                colNames: ['Name', 'Issuer', 'Location', 'Private Key[Yes/No]'],
                //data: dataArray,
                colModel: [
                   {
                       key: false,
                       name: 'Name',
                       index: 'Name',
                       editable: false
                   },
                   {
                       key: false,
                       name: 'Issuer',
                       index: 'Issuer',
                       editable: false
                   },
                   {
                       key: false,
                       name: 'Location',
                       index: 'Location',
                       editable: false
                   },
                   {
                       key: false,
                       name: 'HasPrivateKey',
                       index: 'HasPrivateKey',
                       editable: false
                   }
                ],
                height: '100%',
                viewrecords: true,
                caption: "Certificate Details",
                emptyrecords: "No record to display!!"
            });
        });
控制器代码:

CertDetails cd = new CertDetails();
        public ActionResult Index()
        {
            return View();
        }
        //
        // GET: /CertificateDetails/
        public ActionResult GetCertificateDetails()
        {
            var stores = new Dictionary<StoreName, string>()
                {
                        {StoreName.My, "Personal"},
                        {StoreName.Root, "Trusted roots"},
                        {StoreName.TrustedPublisher, "Trusted publishers"},
                        {StoreName.AddressBook, "Address Book"},
                        {StoreName.AuthRoot, "Auth Root"},
                        {StoreName.CertificateAuthority, "Certificate authority"},
                        {StoreName.Disallowed, "Disallowed"},
                        {StoreName.TrustedPeople, "Trusted people"}
                // and so on
                }.Select(s => new { store = new X509Store(s.Key, StoreLocation.LocalMachine), location = s.Value }).ToArray();

            foreach (var store in stores)
                store.store.Open(OpenFlags.ReadOnly); // open each store

            var list = stores.SelectMany(s => s.store.Certificates.Cast<X509Certificate2>()
                .Select(mCert => new CertDetails
                {
                    HasPrivateKey = mCert.HasPrivateKey ? "Yes" : "No",
                    Name = mCert.FriendlyName != "" ? mCert.FriendlyName : "Unavailable",
                    Location = s.location,
                    Issuer = mCert.Issuer
                })).ToList();

            return Json(list,JsonRequestBehavior.AllowGet);
        }

我从控制器获取JSON格式的数据,但jqGrid既不显示任何数据,也不显示空记录标签。你知道如何解决这个问题吗?

你可以试试jQuery DataTable插件,如下所示:

$(document).ready(function () {
        $("#myGrid").dataTable({
            "ajax": {
                "url": "/CertificateDetails/GetCertificateDetails",
                "dataSrc": ""
            },
            "columns": [{
                "data": "Name"
            }, {
                "data": "Location"
            }, {
                "data": "Issuer"
            }, {
                "data": "HasPrivateKey"
            }]

        });
    });

<table id="myGrid">
    <thead>
        <tr style="text-align:left;">
            <th>Name</th>
            <th>Location</th>
            <th>Issuer</th>
            <th>HasPrivateKey?</th>
        </tr>
    </thead>
      </table>
$(文档).ready(函数(){
$(“#myGrid”).dataTable({
“ajax”:{
“url”:“/CertificatedDetails/GetCertificatedDetails”,
“dataSrc”:”
},
“栏目”:[{
“数据”:“名称”
}, {
“数据”:“位置”
}, {
“数据”:“发行人”
}, {
“数据”:“HasPrivateKey”
}]
});
});
名称
位置
发行人
有私钥吗?
别忘了添加参考资料-

<link rel="stylesheet" type="text/css" href="http://cdn.datatables.net/1.10.9/css/jquery.dataTables.min.css">
<script type="text/javascript" charset="utf8" src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-2.2.0.min.js"></script>
    <script type="text/javascript" charset="utf8" src="http://cdn.datatables.net/1.10.9/js/jquery.dataTables.min.js"></script>


您使用哪个版本的jqGrid以及jqGrid的哪个分支(,商业版或旧版@Oleg中的jqGrid,我使用的是nuget上免费提供的jQuery.jqGrid,版本是4.4.4。我在var列表中获取数据,但它没有显示在网格中。我建议您卸载追溯版4.4.4,它已经过时很久了,并在当前版本中安装nuget软件包:4.14.1测试JSON数据?顺便说一句,我建议您删除
colModel
中不需要的
key:false、editable:false
index
属性,并添加jqGrid选项
loadonce:true,forceClientSorting:true
@Oleg,我已经在action方法返回的问题体中添加了数据。您是否尝试使用免费jqGrid4.14.1?问题是否仍然存在?JSFIDLE允许使用Echo服务(
url:“/Echo/json/”
)来模拟服务器响应。必须将
json
参数与服务器应返回的数据一起发送到服务器。可以使用
postData:{json:…}
要做到这一点,Echo服务需要使用HTTP POST。您可以在演示中看到对代码的最小更改,这很有效。您可以使用更多功能。请参阅。无论如何,我认为您发布的JSON数据没有问题。
<link rel="stylesheet" type="text/css" href="http://cdn.datatables.net/1.10.9/css/jquery.dataTables.min.css">
<script type="text/javascript" charset="utf8" src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-2.2.0.min.js"></script>
    <script type="text/javascript" charset="utf8" src="http://cdn.datatables.net/1.10.9/js/jquery.dataTables.min.js"></script>