I used the below method for doing Async
postback using AJAX
. This works fine on clicking submit
. But i would like to know, is that possible to call various ActionMethod
s in a controller via AJAX
.
I would like to implement something like cascading dropdown. How to call different ActionMethod
via AJAX
on dropdown value change?
Here is the code which call only one ActionMethod
on submitting form.
View
@{
ViewBag.Title = "Index";
var options = new AjaxOptions()
{
Url = Url.Action("Index", "City"),
LoadingElementId = "saving",
LoadingElementDuration = 2000,
Confirm = "Are you sure you want to submit?"
};
}
<h2>Index</h2>
@using (Ajax.BeginForm(options))
{
<div id="saving">Loading...</div>
@Html.DropDownList("Countries",ViewBag.Countries as SelectList)<input type="submit" />
}
Controller
public ActionResult Index()
{
IEnumerable<SelectListItem> selectListItems = new []
{
new SelectListItem{ Text = "US",Value = "1" }
};
ViewBag.Countries = selectListItems;
return View();
}
public ActionResult GetState(string countryId)
{
IEnumerable<SelectListItem> selectListItems = new[]
{
new SelectListItem { Text = "Tennesse", Value = "1" },
new SelectListItem { Text = "Newyork", Value = "2" }
};
return View();
}