0

I have the following code on cshtml page.

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

How do I make this work in my view model?

I think I almost got it, but need some help

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

My viewmodel has this code:

 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.');
                });
            }

I tried this, but I get errors:

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.');
                });
            }

Can someone help me figure this out?

4

1 回答 1

1

The way you're trying to pass in the url to the ajax call is probably not working the way you expect. Also, you wouldn't need the location.href= to be part of the url parameter in the $.ajax() call.

If your view model is coded in a script tag right in your cshtml page, you can try this:

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

Load the page and view source. If the var myUrl = line is the correct URL to your controller as a string, then you know that razor kicked in and prepared that for you on render.

于 2013-05-25T14:08:13.830 回答