使用WebSocket显示和更新来自AJAX调用数据的表

时间:2017-03-31 13:45:15

标签: javascript c# json ajax asp.net-mvc

我尝试在PartialView中显示和更新表格中的数据。我的解决方案无效。

这是我的查看废料Index.cshtml

@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>....

部分视图_CurrenciesTable.cshtml

@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>

还有Controller HomeController.cs

        public ActionResult getWebSocketResults(CurrenciesViewModel currencies)
        {
            if (Request.IsAjaxRequest())
            {
            return PartialView("_CurrenciesTable", currencies.items);
            }
            else
            {
                return View(currencies.items);
            }
        }
        public ActionResult Index()
        {
            return View();
        }

我在调试期间在控制器和部分视图中看到来自ajax的数据,但是表仍然是空的。

测试屏幕

首先是取消部分视图,第二个是表,第三个是来自ajax errof函数的错误 - 我不明白这个错误 Debugging PartialView - data is here

And the home page looks like this

This is error from ajax error function

1 个答案:

答案 0 :(得分:1)

尝试在ajax调用中将dataType: 'json'替换为dataType: 'html'