1

I have two controller

  1. Settings Controller

    Action - GetAvailableLocationsFor

  2. HomeController

    Action - Index

Steps I want to acheive

  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 - Index Action

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

Nested Ajax call

<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>
10
  • 1
    hi do you actually need a ajax call in second time? I mean you can use Url.Action ('','') after success callback with all required data . Commented Jan 17, 2017 at 20:57
  • 1
    return Json(configData, JsonRequestBehavior.AllowGet); and data: data (but this could simply be done using one ajax call to one method - its not clear why you want 2 methods) Commented Jan 17, 2017 at 20:57
  • @YashveerSingh: can you kindly guide me how to achieve that. Commented Jan 17, 2017 at 21:00
  • @StephenMuecke : Thank you for your response. I have some logic in GetAvailableLocationsFor and the Index sometimes needs to be called independent of the logic which is in GetAvailableLocationsFor Commented Jan 17, 2017 at 21:01
  • @StephenMuecke In certain situation I want just the Index logic to be called. And in certain situation I want the logic of GetAvailableLocationsFor to be called and then the index should be called. Commented Jan 17, 2017 at 21:03

2 Answers 2

0

A better approach would be to return a redirect in your GetAvailableLocationsFor action

return RedirectToAction("Index", "Home", configData)

Then in your ajax success instead of the second ajax call. Just handle whatever gets returned by Home/Index.

Sign up to request clarification or add additional context in comments.

7 Comments

You cannot redirect to a POST :)
@StephenMuecke you are absolutely correct. I missed the httppost attribute.
If I take the httppost attribute then this code will work right?
@Unbreakable it depends on what else is calling that action. It might be better to allow both httppost and httpget. For example if you have a form that is submitting to home/index and you don't explicitly tell it to submit via GET it will default to POST and fail if you remove that attribute.
So, I have put both httpost and httpget. Now Can I use your code and get the data at Index Action?
|
0

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

data: JSON.parse(response),

this will get data in your parameter in Index method

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.