MVC3异步取得View
最近,公司又一个新需求,修改现有界面,使用MVC3的方式实现,其中又一个需求是当调用一个Action的时候,按照其返回值,影响多个View的显示,按照最开始使用的是Ajax.BeginForm的方式,发现一次只能影响一个View,后来和其他同事沟通了,使用了$.get方式,代码如下:
<a href="#" onclick="test()">[修改]1</a></div>
<script language="javascript" type="text/javascript">
function test() {
$.get("/SiteNav/_GetAddressList?userID=1", function (data) {
$("#AddressList").html("").append(data);
});
}
</script>
function test() {
$.get("/SiteNav/_GetAddressList?userID=1", function (data) {
$("#AddressList").html("").append(data);
});
}
</script>
Controller
public ActionResult _GetAddressList(int userID)
{
List<Address> addressList = new List<Address>();
addressList.Add(new Address { id="1"});
addressList.Add(new Address{id="2",UserName="测试1"});
addressList.Add(new Address{id="3",UserName="测试2"});
return PartialView("_GetAddressList", addressList);
}
{
List<Address> addressList = new List<Address>();
addressList.Add(new Address { id="1"});
addressList.Add(new Address{id="2",UserName="测试1"});
addressList.Add(new Address{id="3",UserName="测试2"});
return PartialView("_GetAddressList", addressList);
}
直接返回View的Html,按照这里面的值,可以继续调用其他的Action,满足一次点击,多个Action相应。