Asp.net mvc 4 AJAX与MVC4的级联

Asp.net mvc 4 AJAX与MVC4的级联,asp.net-mvc-4,Asp.net Mvc 4,我使用下面的方法使用AJAX进行Async回发。单击submit,这项功能可以正常工作。但是我想知道,是否可以通过AJAX在控制器中调用各种ActionMethods 我想实现一些类似级联下拉列表的功能。如何在下拉值更改时通过AJAX调用不同的ActionMethod 下面是在提交表单时只调用一个ActionMethod的代码 查看 @{ ViewBag.Title = "Index"; var options = new AjaxOptions() { Url = Url.Action

我使用下面的方法使用
AJAX
进行
Async
回发。单击
submit
,这项功能可以正常工作。但是我想知道,是否可以通过
AJAX
在控制器中调用各种
ActionMethod
s

我想实现一些类似级联下拉列表的功能。如何在下拉值更改时通过
AJAX
调用不同的
ActionMethod

下面是在提交表单时只调用一个
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" />
}
@{
ViewBag.Title=“Index”;
var options=new AjaxOptions()
{
Url=Url.Action(“索引”、“城市”),
LoadingElementId=“正在保存”,
LoadingElementDuration=2000,
确认=“您确定要提交吗?”
};  
}
指数
@使用(Ajax.BeginForm(选项))
{
加载。。。
@Html.DropDownList(“国家”,ViewBag.Countries作为选择列表)
}
控制器

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();        
}
public ActionResult Index()
{
IEnumerable selectListItems=new[]
{ 
新建SelectListItem{Text=“US”,Value=“1”}
};
ViewBag.Countries=选择列表项;
返回视图();
}
公共操作结果GetState(字符串countryId)
{
IEnumerable selectListItems=new[]
{ 
新建SelectListItem{Text=“Tennesse”,Value=“1”},
新建SelectListItem{Text=“Newyork”,Value=“2”}
};
返回视图();
}

第一个问题的答案是“是否可以通过AJAX在控制器中调用各种ActionMethods”是一个很大的问题。您可以通过Ajax从控制器调用任何操作方法,尽管生成的唯一结果取决于各种情况,如是否发送视图、部分视图或JSON结果

关于你的下一个问题:

我将张贴一些代码

Controller.cs

public JsonResult getCity(string country)
    {
        var temp = (from cntry in db.Table3.OrderBy(s => s.country)
                    where (string.Compare(cntry.country, country) == 0)
                    select cntry.city).ToList();
        return Json(temp, JsonRequestBehavior.AllowGet);
    }
查看

<h1>
Countries</h1>
<select name="countries" class="combo">
<option value=""></option>

@{
    foreach (var t in (List<string>)ViewBag.countries)
    {
    <option value=@t>@t</option>
    }
}
 </select>
<h1>
State</h1>
<select name="city" class="combo2">
</select>
<div id="tese">
</div>
@*
The following jquery code finds the selected option from country dropdown 
and then sends an ajax call to the Home/getcity method 
and finally populate it to the city dropdown 
*@
<script type="text/javascript">
$('body').on('change', '.combo', function () {
    var selectedValue = $(this).val();
    alert(selectedValue);
    $.get("/Home/getcity", { country: selectedValue }, function (data) {
        $("#tese").html(data);
        $(".combo2").html("<option value = \"\"></option>")
        $.each(data, function (index, value) {
            $(".combo2").append("<option value = \"" + value + "\">" + value + "</option>");
        });
        $(".combo2").html()
    });
});
</script>

国家
@{
foreach(列表中的var t ViewBag.countries)
{
@t
}
}
陈述
@*
以下jquery代码从国家/地区下拉列表中查找所选选项
然后向Home/getcity方法发送ajax调用
最后将其填充到城市下拉列表中
*@
$('body').on('change','.combo',函数(){
var selectedValue=$(this.val();
警报(selectedValue);
$.get(“/Home/getcity”,{country:selectedValue},函数(数据){
$(“#tese”).html(数据);
$(“.combo2”).html(“”)
$.each(数据、函数(索引、值){
$(“.combo2”).append(“+value+”);
});
$(“.combo2”).html()
});
});
这将显示国家列表的下拉列表。一旦选择一个国家,它将呈现一个新的城市列表下拉列表

public JsonResult getCity(string country)
    {
        var temp = (from cntry in db.Table3.OrderBy(s => s.country)
                    where (string.Compare(cntry.country, country) == 0)
                    select cntry.city).ToList();
        return Json(temp, JsonRequestBehavior.AllowGet);
    }
看法


国家
@{
foreach(列表中的var t ViewBag.countries)
{
@t
}
}
陈述
@*
以下jquery代码从国家/地区下拉列表中查找所选选项
然后向Home/getcity方法发送ajax调用
最后将其填充到城市下拉列表中
*@
$('body').on('change','.combo',函数(){
var selectedValue=$(this.val();
警报(selectedValue);
$.get(“/Home/getcity”,{country:selectedValue},函数(数据){
$(“#tese”).html(数据);
$(“.combo2”).html(“”)
$.each(数据、函数(索引、值){
$(“.combo2”).append(“+value+”);
});
$(“.combo2”).html()
});
});

检查,如果仍然存在任何问题,请对答案进行评论,或者将其作为答案接受
<h1>
Countries</h1>
<select name="countries" class="combo">
<option value=""></option>

@{
    foreach (var t in (List<string>)ViewBag.countries)
    {
    <option value=@t>@t</option>
    }
}
 </select>
<h1>
State</h1>
<select name="city" class="combo2">
</select>
<div id="tese">
</div>
@*
The following jquery code finds the selected option from country dropdown 
and then sends an ajax call to the Home/getcity method 
and finally populate it to the city dropdown 
*@
<script type="text/javascript">
$('body').on('change', '.combo', function () {
    var selectedValue = $(this).val();
    alert(selectedValue);
    $.get("/Home/getcity", { country: selectedValue }, function (data) {
        $("#tese").html(data);
        $(".combo2").html("<option value = \"\"></option>")
        $.each(data, function (index, value) {
            $(".combo2").append("<option value = \"" + value + "\">" + value + "</option>");
        });
        $(".combo2").html()
    });
});
</script>