weixin_33739541 2019-04-24 17:44 采纳率: 0%
浏览 100

通过JQuery访问控制器

I have a view called InvoiceTo and a Controller called Order. Using JQuery, the default URL is: url: '', and this is the result:

locahost:port/domain/order/InvoiceTo

If I change the URL: url: /Order/GetInformation, this is the result:

locahost:port/domain/order/InvoiceTo/Order/GetInformation

I've already tried lots of way to set my url, but is always wrong. This is my JQuery:

$(document).ready(function () {
        $('#InvoiceToDrop').change(function () {
            var $div = $('#modalPartial'); 
            var idcustomer = $(this).val(); 
            $.ajax({
                url: '/Order/GetInformation/' + idcustomer,
                type: 'GET',                
                success: function (data) {
                    alert(JSON.stringify(data));
                },
                error: function (error) {
                }
            })
        });
    });

What I have to do to have this URL:

locahost:port/domain/Order/GetInformation/1

  • 写回答

1条回答 默认 最新

  • weixin_33698823 2019-04-24 17:57
    关注

    You can use the UrlHelper Url.Action method in the view:

    $.ajax({ 
        url: '@Url.Action("GetInformation", "Order", new { customerId })',
        ...
    

    Or

    url: '@Url.Action("GetInformation", "Order")/' + idcustomer
    

    When the view loads @Url.Action is parsed and is replaced by the actual value.

    Advantages of using this method are that it uses the routing table rather than statically typed urls.

    评论

报告相同问题?