根据MVC将json数据显示为表

时间:2015-12-31 06:40:41

标签: c# jquery .net asp.net-mvc asp.net-mvc-4

我在MVC 4应用程序中工作。我正在尝试将json数据绑定到视图中的表,该表在View页面中反映为表视图。

这是我的控制器代码....

stdout

结果是一个数据表,它将输出数据保存为数据表。 this is the table in result

将表传递给View我将表序列化为:

if (reportType == "Report")
{
    result = client.GetApiRequest("api/TurnoverReport/get?packageID=" + Convert.ToInt32(packageID)).Result;
}

这里我正在使用jsonconvert,因为我有像

这样的错误消息
  

在序列化“System.Globalization.CultureInfo”类型的对象时检测到循环引用。

所以,这个json将数据返回为: the data that is returning the json to View

我试图在View中显示,但我无法获得预期的结果.. 这是View代码;

System.Web.Script.Serialization.JavaScriptSerializer serializer = new System.Web.Script.Serialization.JavaScriptSerializer();
            List<Dictionary<string, object>> rows = new List<Dictionary<string, object>>();
            Dictionary<string, object> row = new Dictionary<string, object>();
            row.Add("text", result);
            rows.Add(row);

var test = JsonConvert.SerializeObject(rows, Formatting.Indented,
                        new JsonSerializerSettings
                        {
                            ReferenceLoopHandling = ReferenceLoopHandling.Ignore
                        });

return Json(serializer.Serialize(test), JsonRequestBehavior.AllowGet);

1 个答案:

答案 0 :(得分:1)

你可以在你的javascript方法中尝试这个

string devicetocken = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";//  iphone device token
int port = 2195;
String hostname = "gateway.sandbox.push.apple.com";
string certificatePath = @"Certificates.pfx";
string certificatePassword = "";
X509Certificate2 clientCertificate = new X509Certificate2(certificatePath, certificatePassword, X509KeyStorageFlags.MachineKeySet);
X509Certificate2Collection certificatesCollection = new X509Certificate2Collection(clientCertificate);

TcpClient client = new TcpClient(hostname, port);
SslStream sslStream = new SslStream(client.GetStream(),false,new RemoteCertificateValidationCallback(ValidateServerCertificate),null);
try
{
    sslStream.AuthenticateAsClient(hostname, certificatesCollection, SslProtocols.Default, false);
}
catch (AuthenticationException ex)
{
    Console.WriteLine("Authentication failed");
    client.Close();
    return;
}
MemoryStream memoryStream = new MemoryStream();
BinaryWriter writer = new BinaryWriter(memoryStream);
writer.Write((byte)0);  //The command
writer.Write((byte)0);  //The first byte of the deviceId length (big-endian first byte)
writer.Write((byte)32); //The deviceId length (big-endian second byte)
int len = devicetocken.Length;
int len_half = len / 2;
byte[] bs = new byte[len_half];
for (int i = 0; i != len_half; i++)
{
    bs[i] = (byte)Int32.Parse(devicetocken.Substring(i * 2, 2), System.Globalization.NumberStyles.HexNumber);
}
byte[] b0 = bs;
writer.Write(b0);
String payload;
string strmsgbody = "";
strmsgbody = "சென்னை";
//strmsgbody = "Chennai";
Debug.WriteLine("during testing via device!");
payload = "{\"aps\":{\"alert\":\"" + strmsgbody + "\",\"sound\":\"mailsent.wav\"},\"acme1\":\"bar\",\"acme2\":42}";
writer.Write((byte)0); //First byte of payload length; (big-endian first byte)
writer.Write((byte)payload.Length);     //payload length (big-endian second byte)
byte[] b1 = System.Text.Encoding.UTF8.GetBytes(payload);
writer.Write(b1);
writer.Flush();
byte[] array = memoryStream.ToArray();
Debug.WriteLine("This is being sent...\n\n");
Debug.WriteLine(array);
try
{
    sslStream.Write(array);
    sslStream.Flush();
}
catch
{
    Debug.WriteLine("Write failed buddy!!");
}

client.Close();
Debug.WriteLine("Client closed.");