簡體   English   中英

ASP.NET MVC調用帶有JavaScript函數參數的Controller Action

[英]ASP.NET MVC call Controller Action with parameter from javascript function

我在ASP.NET MVC C#中有Web應用程序。 我想使用來自JavaScript的參數調用Controller Action方法,但參數始終為null。

在.js中,我有:

$.ajax({
    cache: false,
    url: this.saveUrl,
    type: 'post',
    success: this.nextStep,
    complete: this.resetLoadWaiting,
    error: Checkout.ajaxFailure
});

nextStep: function (response) {
    if (response.error) {
        if ((typeof response.message) == 'string') {
            alert(response.message);
        } else {
            alert(response.message.join("\n"));
        }

        return false;
    }

    if (response.redirect) {
        ConfirmOrder.isSuccess = true;
        location.href = response.redirect;
        return;
    }
    if (response.success) {
        ConfirmOrder.isSuccess = true;
        window.location = ConfirmOrder.successUrl;
        //window.location.href = @Url.Action("Completed", "Checkout", new { customerComment = "test", customerDate = "2018-12-31" });
        //window.location.href = '@Html.GetUrl("Completed", "Checkout", new { customerComment = "test", customerDate = "2018-12-31" })';
    }

    Checkout.setStepResponse(response);
}

在RouteProvider.cs中,我有:

        routes.MapLocalizedRoute("CheckoutCompleted",
                        "checkout/completed/{orderId}/{customerComment}/{customerDate}",
                        new { controller = "Checkout", action = "Completed", orderId = UrlParameter.Optional, customerComment = UrlParameter.Optional, customerDate = UrlParameter.Optional },
                        new { orderId = @"\d+", customerComment = @"\w+", customerDate = @"\w+" },
                        new[] { "Nop.Web.Controllers" });

最后,在Controller中,我有:

    public virtual ActionResult Completed(int? orderId, string customerComment, string customerDate)
    {
        //some code here
    }

我總是得到參數值null,我不知道為什么。 我嘗試以幾種不同的方式使用參數來調用此Action,就像我在該論壇上看到的那樣,但是沒有成功。

您是否將httpPost包裝器添加到您的控制器?

[HttpPost]
     public virtual ActionResult Completed(MyClass  MyClassObj )
     {
            //some code here
     }

在您的Ajax代碼中,您沒有提及您的數據類型和數據,請嘗試使用此Ajax

function Save () {

            try {

                var req = {                   
                    DeliveryCode: value,

                }

                $.ajax({
                    url: URL,
                    type: 'POST',
                    data: req,
                    dataType: "json",
                    async: true,
                    success: function (result) {

                        resetLoadWaiting()

                    },
                    error: function (e) {

                    }
                });
            }
            catch (e) {


            }
        }  

錯誤在於網址字串。 正確的是

ConfirmOrder.successUrl =“ http:// localhost:8079 / checkout / completed /?customerComment = eee&customerEstimateShippingDate = 2017-11-14

我在以下答案中找到了此解決方案: 使用ASP.NET MVC進行多個參數的路由

我不需要用新參數更新RouteProvider。 放入Controller Action方法就足夠了。 其他事情會自動發生。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM