0
在控制器操作即時獲取多個記錄,即時通訊不能顯示在表中。此代碼適用於單個記錄,顯示不適用於多個記錄。如何爲多個記錄使用集合。如何顯示從控制器獲取數據從流讀取器在C#中的多個記錄#
樣本數據
[{"id":1488437716804,"timestamp":"2017-03-02T06:55:16.804Z","dataFrame":"UkFEOkRO","fcnt":12453,"port":2,"rssi":-83,"snr":8.5,"sf_used":7,"decrypted":true},{"id":1488437738877,"timestamp":"2017-03-02T06:55:38.877Z","dataFrame":"UkFEOkRO","fcnt":12455,"port":2,"rssi":-87,"snr":9.80000019073486,"sf_used":7,"decrypted":true}]
控制器
private List<PIRDetailsViewModel> getPirDetails()
{
List<PIRDetailsViewModel> model = new List<PIRDetailsViewModel>();
try
{
WebRequest req = WebRequest.Create(@"https:url");
req.Method = "GET";
req.Headers["Authorization"] = "Basic " + "a2VybmVsc3BoZXJlOmtlcm5lbHNwaGVyZQ==";
HttpWebResponse resp = req.GetResponse() as HttpWebResponse;
var encoding = resp.CharacterSet == "" ? Encoding.UTF8 : Encoding.GetEncoding(resp.CharacterSet);
using (var stream = resp.GetResponseStream())
{
var reader = new StreamReader(stream, encoding);
var responseString = reader.ReadToEnd();
dynamic bin = Newtonsoft.Json.JsonConvert.DeserializeObject(responseString);
byte[] data = Convert.FromBase64String(bin.dataFrame.ToString());
PIRDetailsViewModel binModel = new PIRDetailsViewModel();
binModel.status = Convert.ToString(Encoding.UTF8.GetString(data).Substring(4));
binModel.UpdatedTime = TimeZoneInfo.ConvertTimeFromUtc(Convert.ToDateTime(bin.timestamp), TimeZoneInfo.FindSystemTimeZoneById("India Standard Time")).ToString();
model.Add(binModel);
}
}
return model;
}
視圖
<table class="table table-striped" id="listing">
<thead>
<tr class="success">
<th class="success" style="font-size:20px">sl no</th>
<th class="success" style="font-size:20px">Time</th>
<th class="success" style="font-size:20px">status</th>
</tr>
</thead>
<tbody>
@foreach (var item in Model.Select((value, i) => new { i, value }))
{
<tr id="[email protected]">
<td class="danger">@item.value.status</td>
<td class="warning">@item.value.UpdatedTime</td>
</tr>
}
</tbody>
</table>
您需要使用集合並將其綁定到模型。在視圖中,您必須使用綁定在模型中的集合並將其呈現爲網格/表格 –