如何调用嵌套的Ajax调用并将对象数据发送到ASP.Net MVC 5中的控制器 [英] How to call nested ajax call and send object data to controller in ASP.Net MVC 5

查看:176
本文介绍了如何调用嵌套的Ajax调用并将对象数据发送到ASP.Net MVC 5中的控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个控制器

  1. 设置控制器

  1. Settings Controller

操作-GetAvailableLocationsFor

Action - GetAvailableLocationsFor

HomeController

HomeController

操作-索引

我要实现的步骤

  1. GetAvailableLocationsFor进行ajax调用,然后从成功的回调中获取对象数据.此操作不需要查看.
  2. 现在,在接收到对象数据的情况下,再次对HOMECONTROLLER中的Index Action进行ajax调用,然后将对象传递到那里.
  1. Make ajax call to GetAvailableLocationsFor and then get the object data from success call back. No view is required for this Action.
  2. Now with the object data received make another ajax call to Index Action in HOMECONTROLLER and pass the object there.

以下是我可以实现的目标.

Below is what I could achieve.

HomeController-GetAvailableLocationsFor

public ActionResult GetAvailableLocationsFor(int accountId, int groupId)
        {
            FullConfigMV configData = SetLoader.GetSettings(accountId, groupId);
           return // HOW TO RETURN configData from Here to first ajax call
}

HomeController-索引操作

[HttpPost]
public ActionResult Index(FullConfigMV data)
{
  //SECOND AJAX CALL SHOULD COME HERE
}

嵌套Ajax呼叫

<script>
    $(document).ready(function()
    {
        $("#tan").change(function()
        {
            alert(this.value);
            $.ajax({
                type: 'POST',
                url: '/Settings/GetAvailableLocationsFor',
                data: { accountId: 28462, groupId: 35},
                success: function (data) { // data should represent configObj

                    $.ajax({
                        type: 'POST',
                        url: '/Home/Index',
                        data: // WHAT TO WRITE HERE,
                        success: function (data) {
                            //WHATEVER
                        },
                        error: function () {
                            DisplayError('Failed to load the data.');
                        }
                    });

                },
                error: function () {
                    DisplayError('Failed to load the data.');
                }
            });


        });
    });
</script>

推荐答案

GetAvailableLocationsFor方法返回JSON数据.在第二次ajax调用之后,您可以发送如下数据:

Return JSON data from GetAvailableLocationsFor method. After in second ajax call you can send data like below:

data: JSON.parse(response),

这将在Index方法中获取参数中的数据

this will get data in your parameter in Index method

这篇关于如何调用嵌套的Ajax调用并将对象数据发送到ASP.Net MVC 5中的控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆