我使用以下方法Async
使用AJAX
. 这在单击时工作正常submit
。但我想知道,是否可以ActionMethod
通过AJAX
.
我想实现类似级联下拉菜单的东西。如何ActionMethod
通过AJAX
下拉值更改调用不同的?
ActionMethod
这是在提交表单时只调用一个的代码。
看法
@{
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" />
}
控制器
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();
}