简体   繁体   中英

Passing an object to MVC controller method in AJAX success using window.location

Ajax call -

        $.ajax({
            type: "POST",
            url: configMap.sitePath + "api/Quiz/" + quizResponse.quizId,
            data: JSON.stringify(quizResponse),
            success: function(data) {
                var resultid = data.data.quizInstanceId;
                var resultresponse = data.data;
                window.location.href = "supply-chain-pressure/ScptResult/" + resultresponse;
            }

controller Method -

   public ActionResult ScptResult(QuizResult resultresponse)
    {
         // Do something
        return View();
    }

Route -

    routes.MapRoute(
        name: "SupplyChainPressureResult",
        //url: "supply-chain-pressure/ScptResult/{resultid}",
        url: "supply-chain-pressure/ScptResult/{resultresponse}",
        defaults: new { controller = "SupplyChainPressure", action = "ScptResult" }
        );

class -

public class QuizResult
    {
        public int quizInstanceId { get; set; }
        public string WeakestLinkCode { get; set; }
        public string WeakestLinkDescription { get; set; }
        public decimal PercentageOfUsers { get; set; }
        public IEnumerable<OverallResult> OverallResults { get; set; }
    }

But For some reason the resultresponse is null in the controller method. Can someone help please . We need to pass data.data ( resultresponse ) to the controller Method.

Thanks Every one for the Help . I only using one parameter and then redirecting to a controller methods which does the stuff for me -

 $.ajax({
                    type: "POST",
                    url: configMap.sitePath + "api/Quiz/" + quizResponse.quizId,
                    data: JSON.stringify(quizResponse),
                    success: function (data) {
                        var resultId = data.data.quizInstanceId;
                        window.location.href = configMap.sitePath + 'Result-Display/Index/' + resultId;
                       },

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM