使用window.location将对象传递给AJAX成功的MVC控制器方法

时间:2015-04-07 15:54:21

标签: c# jquery ajax asp.net-mvc

Ajax调用 -

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

控制器方法 -

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

路线 -

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

但由于某些原因,resultresponse在控制器方法中为空。请有人帮忙吗。我们需要将data.dataresultresponse)传递给控制器​​方法。

1 个答案:

答案 0 :(得分:0)

谢谢每一个人的帮助。我只使用一个参数,然后重定向到一个控制器方法,为我做的事情 -

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