【问题标题】:knockout.js redirect in view model视图模型中的 knockout.js 重定向
【发布时间】:2013-05-25 22:08:13
【问题描述】:

我在cshtml页面上有以下代码。

 <div class="buttons">
    <button type="button" id="export" class="export-inventory-button" onclick="location.href='@Url.Action("ExportInventory", "Inventory")'">EXPORT INVENTORY</button>
</div>

如何在我的视图模型中进行这项工作?

我想我差不多明白了,但需要一些帮助

<div class="buttons">
    <button type="button" id="export" class="export-inventory-button" data-bind="click: exportInventory">EXPORT INVENTORY</button>
</div>

我的视图模型有这个代码:

 function exportInventory() {
                filtererGridData = vm.details;

                var json = ko.mapping.toJSON(vm.details);

                $.ajax({ url: '/Inventory/ExportInventory', type: 'POST' }).done(function (data) {
                    $('#export').html(data);
                }).fail(function (data) {
                    toastr.warn('Could not export data, please contact LGL.');
                });
            }

我试过这个,但我得到了错误:

function exportInventory() {
                filtererGridData = vm.details;

                var json = ko.mapping.toJSON(vm.details);

                $.ajax({ url: 'location.href="@Url.Action("ExportInventory", "Inventory")"', type: 'POST' }).done(function (data) {
                    window.location.href = responseText.url;
                    $('#export').html(data);
                }).fail(function (data) {
                    toastr.warn('Could not export data, please contact LGL.');
                });
            }

谁能帮我解决这个问题?

【问题讨论】:

  • 你会遇到什么错误?
  • 第一个只是将数据转储到屏幕上。第二个返回 404 错误。 Url not found ="@Url.Action("ExportInventory", "Inventory")。我认为它无法正确解析行。
  • 你的 javascript 在你的 cshtml 页面中吗?
  • 函数exportInventory在viewmodel中

标签: asp.net-mvc razor knockout.js


【解决方案1】:

您尝试将 url 传递给 ajax 调用的方式可能没有按您预期的方式工作。此外,您不需要 location.href= 成为 $.ajax() 调用中 url 参数的一部分。

如果您的视图模型是在您的 cshtml 页面中的脚本标记中编码的,您可以试试这个:

<!-- cshtml razor view code for generating the html is above this line -->

<script>
    var viewModel = {
        function exportInventory() {
            filtererGridData = vm.details;

            var json = ko.mapping.toJSON(vm.details);

            //allow razor to build a javascript string for you when it renders the html
            //when the browser parses this script, it will just see a simple string
            var myURL = '@Url.Action("ExportINventory", "Inventory")';

            //pass your variable to the jQuery ajax call
            $.ajax({ url: myURL, type: 'POST' }).done(function (data) {
                window.location.href = responseText.url;
                //this line of code would never be called because the browser has navigated away from this page...
                $('#export').html(data);
            }).fail(function (data) {
                toastr.warn('Could not export data, please contact LGL.');
            });
        }
    };
</script>

加载页面并查看源代码。如果 var myUrl = 行是作为字符串的控制器的正确 URL,那么您知道 razor 启动并在渲染时为您准备好了。

【讨论】:

    猜你喜欢
    • 2014-01-03
    • 2015-07-16
    • 1970-01-01
    • 1970-01-01
    • 2017-09-01
    • 2014-10-04
    • 2017-09-01
    • 2017-08-28
    • 2012-12-11
    相关资源
    最近更新 更多