0

I have web application in ASP.NET MVC C#. I want to call Controller Action method with parameters from javascript but I get always null for parameters.

In .js I have:

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

in RouteProvider.cs I have:

        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" });

And finaly in Controller I have:

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

I always get parameters value null and I don't know why. I try to call this Action with parameters on several diferent way like I saw on this forum but no success.

Korl
  • 95
  • 3
  • 15
  • "I get always null for parameters" where ? and which parameters? – DaniDev Nov 21 '17 at 19:59
  • In Controller for parameter customerComment and customerDate. In javascript function is correct and I generate ComfirmOrder.successUrl like: http:// localhost:1234/checkout/completed/?customerComment='test'&customerDate='2017-12-25'. I tried also with window.location.href = @Url.Action("Completed", "Checkout", new { customerComment = "test", customerDate = "2018-12-31" }); and window.location.href = '@Html.GetUrl("Completed", "Checkout", new { customerComment = "test", customerDate = "2018-12-31" })'; but doesn't work eather. – Korl Nov 22 '17 at 00:26
  • if you put a break point in controller, does it hit it? – DaniDev Nov 22 '17 at 00:42
  • Check your AJAX, you're also performing a 'POST' method, write [HttpPost] above your method in your controller. – Thadeu Fernandes Nov 22 '17 at 02:25
  • AJAX part is OK and I get there all data I need. Problematic is call Controller Action in if(response.success). These two try doesn't bring parameter to the Action method. I come to Controller Action method but parameter customerComment and customerData are null. Currently I have ConfirmOrder.successUrl = 'http://localhost:12345/checkout/completed/test/2017-12-25 doesnt work, eather call window.location.href = '@Html.GetUrl("Completed", "Checkout", new { customerComment = "test", customerDate = "2018-12-31" })'; How should I call my Controller Action corectly? – Korl Nov 22 '17 at 08:11

2 Answers2

0

Did you add httpPost wrapper to your controller ?

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

in your Ajax code u didn't mention your Data type And Data try This 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) {


            }
        }  
Floxy
  • 117
  • 2
  • 10
  • No I dont have it. In case I put it than I dont reach Controller Action method. Problematic is call to Controller Action in if(response.success). I come to Controller Action method but parameter customerComment and customerData are null. Currently I use ConfirmOrder.successUrl = 'localhost:12345/checkout/completed/test/2017-12-25 but doesnt work, eather call window.location.href = '@Html.GetUrl("Completed", "Checkout", new { customerComment = "test", customerDate = "2018-12-31" })'; doesnt work. Question is How should I call my Controller Action correctly? – Korl Nov 22 '17 at 08:21
0

Mistake was in url string. Correct one is

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

I found this solution in this answer: Routing with Multiple Parameters using ASP.NET MVC

I dont need to update RouteProvider with new parameters. It is enough to put in in Controller Action method. Other things happen automatically.

Korl
  • 95
  • 3
  • 15