问题
I have two controller
Settings Controller
Action - GetAvailableLocationsFor
HomeController
Action - Index
Steps I want to acheive
- Make ajax call to
GetAvailableLocationsFor
and then get the object data from success call back. No view is required for this Action. - 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>
回答1:
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.
回答2:
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
来源:https://stackoverflow.com/questions/41706791/how-to-call-nested-ajax-call-and-send-object-data-to-controller-in-asp-net-mvc-5