我尝试在PartialView中显示和更新表格中的数据。我的解决方案无效。
@model IEnumerable<Exchange.WebUI.Models.Item>
@{
ViewBag.Title = "Home Page";
}
<script type="text/javascript">
var webSocket;
function webSocketResults() {
webSocket = new WebSocket("ws://....");
webSocket.onmessage = function (event) {
showCurrenciesData(event.data);
$("#webSocketValue").text(event.data);
};
}
function showCurrenciesData(data) {
$.ajax({
type: 'POST',
url: "@Url.Action("getWebSocketResults", "Home")",
dataType: 'json',
contentType: 'application/json; charset=utf-8',
data: data,
success: function(data) {
$("#currenciesTable").html(data);
},
error: function (xhr, ajaxOptions, thrownError) {
console.log(thrownError, xhr, ajaxOptions);
}
});
}
window.onload = webSocketResults;
</script>
<div class="row">
<div class="col-md-6 well well-sm">
<h2>Currencies</h2>
<hr>
@Html.Partial("_CurrenciesTable", Model)
</div>....
@model IEnumerable<Exchange.WebUI.Models.Item>
@{
ViewBag.Title = "_CurrenciesTable";
}
<div id="currenciesTable">
<table class="table table-striped">
<thead>
<tr>
<th>
@Html.DisplayNameFor(model => model.name)
</th>
<th>
@Html.DisplayNameFor(model => model.code)
</th>
<th>
@Html.DisplayNameFor(model => model.unit)
</th>
<th>
@Html.DisplayNameFor(model => model.purchasePrice)
</th>
<th>
@Html.DisplayNameFor(model => model.sellPrice)
</th>
<th>
@Html.DisplayNameFor(model => model.averagePrice)
</th>
</tr>
</thead>
@if (Model != null)
{
foreach (var item in Model)
{
<tbody>
<tr>
<td>
@Html.DisplayFor(modelItem => item.name)
</td>
<td>
@Html.DisplayFor(modelItem => item.code)
</td>
<td>
@Html.DisplayFor(modelItem => item.unit)
</td>
<td>
@Html.DisplayFor(modelItem => item.purchasePrice)
</td>
<td>
@Html.DisplayFor(modelItem => item.sellPrice)
</td>
<td>
@Html.DisplayFor(modelItem => item.averagePrice)
</td>
</tr>
</tbody>
}
}
else
{
<tbody>
<tr>
<td>
No Connection:
</td>
</tr>
</tbody>
}
</table>
</div>
public ActionResult getWebSocketResults(CurrenciesViewModel currencies)
{
if (Request.IsAjaxRequest())
{
return PartialView("_CurrenciesTable", currencies.items);
}
else
{
return View(currencies.items);
}
}
public ActionResult Index()
{
return View();
}
我在调试期间在控制器和部分视图中看到来自ajax的数据,但是表仍然是空的。
答案 0 :(得分:1)
尝试在ajax调用中将dataType: 'json'
替换为dataType: 'html'
。