Javascript 为什么不是';当通过Ajax调用调用我的控制器时,我的页面不会刷新
下面是我的标记,它执行对控制器的JavaScript/ajax回调。我试图做的是让这个点击功能激活一个选项卡组件,它会这样做,并在控制器中执行操作Javascript 为什么不是';当通过Ajax调用调用我的控制器时,我的页面不会刷新,javascript,asp.net-mvc,Javascript,Asp.net Mvc,下面是我的标记,它执行对控制器的JavaScript/ajax回调。我试图做的是让这个点击功能激活一个选项卡组件,它会这样做,并在控制器中执行操作 <ul class="nav nav-tabs"> <li class="active"><a href="#tab1" onclick="setCert(0);" data-toggle="tab">Registered</a></li> <li><a h
<ul class="nav nav-tabs">
<li class="active"><a href="#tab1" onclick="setCert(0);" data-toggle="tab">Registered</a></li>
<li><a href="#tab2" onclick="setCert(1);" data-toggle="tab">Certified</a></li>
</ul>
最后是我的控制器:
public ActionResult CommunitiesLanding(int id)
{
var model = new CommunitiesViewModel();
var comm = new List<CommunityPoints>();
var mapPoints = new List<CommunityPoints>();
var mapPoints2 = new List<CommunityPoints>();
var regComm = new List<Registered>();
var certComm = new List<Certified>();
var locationService = new GoogleLocationService();
var communites = db.Communities.Where(x => x.Certified != true).OrderBy(x => x.CommunityState).ToList();
var certCommunities = db.Communities.Where(x => x.Certified == true).OrderBy(x => x.CommunityState).ToList();
var statecd = communites[0];
var statecd2 = statecd.CommunityState;
if (id == 0)
{
// Collect the Registered communites data
foreach (var c in communites)
{
if (statecd2 != c.CommunityState)
{
var reg = new Registered();
reg.state = statecd2;
reg.points = comm;
regComm.Add(reg);
comm = new List<CommunityPoints>();
statecd2 = c.CommunityState;
}
var communityPts = new CommunityPoints();
var points = locationService.GetLatLongFromAddress(c.CommunityZip);
communityPts.CommunityId = c.CommunityId;
communityPts.CommunityName = c.ComunityName;
communityPts.latitude = points.Latitude.ToString();
communityPts.longitude = points.Longitude.ToString();
communityPts.state = c.CommunityState;
comm.Add(communityPts);
mapPoints.Add(communityPts);
}
// Collect the very last collection of state data
var Lastreg = new Registered();
Lastreg.state = statecd2;
Lastreg.points = comm;
comm = new List<CommunityPoints>();
regComm.Add(Lastreg);
}
else
{
// Collect Data For the Certified Communites
statecd = certCommunities[0];
statecd2 = statecd.CommunityState;
foreach (var c in certCommunities)
{
if (statecd2 != c.CommunityState)
{
var cert = new Certified();
cert.state = statecd2;
cert.points = comm;
certComm.Add(cert);
comm = new List<CommunityPoints>();
statecd2 = c.CommunityState;
}
var communityPts = new CommunityPoints();
var points = locationService.GetLatLongFromAddress(c.CommunityZip);
communityPts.CommunityId = c.CommunityId;
communityPts.CommunityName = c.ComunityName;
communityPts.latitude = points.Latitude.ToString();
communityPts.longitude = points.Longitude.ToString();
communityPts.state = c.CommunityState;
comm.Add(communityPts);
mapPoints2.Add(communityPts);
}
// Collect the very last collection of state data
var Lastcert = new Certified();
Lastcert.state = statecd2;
Lastcert.points = comm;
comm = new List<CommunityPoints>();
certComm.Add(Lastcert);
}
model.regCommunities = regComm;
model.cerCommunities = certComm;
model.regPoints = mapPoints;
model.certPoints = mapPoints2;
return View(model);
}
public ActionResult社区登录(int-id)
{
var模型=新社区视图模型();
var comm=新列表();
var mapPoints=新列表();
var mapPoints2=新列表();
var regComm=新列表();
var certComm=新列表();
var locationService=new GoogleLocationService();
var communites=db.Communities.Where(x=>x.Certified!=true).OrderBy(x=>x.CommunityState.ToList();
var certCommunities=db.Communities.Where(x=>x.Certified==true).OrderBy(x=>x.CommunityState.ToList();
var statecd=通信[0];
var statecd2=statecd.CommunityState;
如果(id==0)
{
//收集已注册的社区数据
foreach(社区中的变量c)
{
if(statecd2!=c.CommunityState)
{
var reg=新注册的();
reg.state=statecd2;
注册点=通信;
注册通讯地址(注册号);
comm=新列表();
statecd2=社区状态;
}
var communityPts=新的CommunityPoints();
var points=locationService.GetLatLongFromAddress(c.CommunityZip);
communityPts.CommunityId=c.CommunityId;
communityPts.CommunityName=c.ComunityName;
communityPts.latitude=points.latitude.ToString();
communityPts.longitude=points.longitude.ToString();
communityPts.state=c.communitypstate;
通信添加(通信类型);
mapPoints.Add(communityPts);
}
//收集状态数据的最后一次收集
var Lastreg=新注册();
Lastreg.state=statecd2;
Lastreg.points=通信;
comm=新列表();
regComm.Add(Lastreg);
}
其他的
{
//为认证社区收集数据
statecd=certCommunities[0];
statecd2=statecd.CommunityState;
foreach(社区中的var c)
{
if(statecd2!=c.CommunityState)
{
var cert=新认证();
cert.state=statecd2;
证书点=通信;
证书通信添加(证书);
comm=新列表();
statecd2=社区状态;
}
var communityPts=新的CommunityPoints();
var points=locationService.GetLatLongFromAddress(c.CommunityZip);
communityPts.CommunityId=c.CommunityId;
communityPts.CommunityName=c.ComunityName;
communityPts.latitude=points.latitude.ToString();
communityPts.longitude=points.longitude.ToString();
communityPts.state=c.communitypstate;
通信添加(通信类型);
mapPoints2.Add(communityPts);
}
//收集状态数据的最后一次收集
var Lastcert=new Certified();
Lastcert.state=statecd2;
Lastcert.points=通信;
comm=新列表();
证书通信添加(最新证书);
}
model.regCommunities=regComm;
model.cerCommunities=certComm;
model.regPoints=mapPoints;
model.certPoints=mapPoints2;
返回视图(模型);
}
如果您的目标是发出AJAX请求以初始化某些数据,然后在服务器完成处理后将用户重定向到新初始化的页面,则可以更新AJAX函数以在成功时重定向:
function setCert(cert) {
var url = '/Home/CommunitiesLanding/' + cert;
$.ajax({
url: url,
type: "GET",
traditional: true,
contentType: "application/json",
success: function () {
// redirect user to URL
location.href = url;
}
});
}
不过,如果不需要初始化数据并等待数据提前完成,直接重定向用户可能更有意义:
function setCert(cert) {
location.href = '/Home/CommunitiesLanding/' + cert;
}
您使用的是什么MVC框架?为什么希望页面刷新?您正在进行AJAX调用(不涉及浏览器在任何地方导航用户),并且在成功时只执行
控制台.log
。这是在使用.NET Framework。我意识到我正在进行AJAX调用,也许您可以指导我正确的方向。我想通过再次执行控制器刷新页面。如果要动态加载来自控制器的选项卡内容,请考虑使用部分视图。为了得到这个想法,我想知道这些相同的观点,如果尝试在页面的某个部分做一些SPA,结果…那恐怕是行不通的。它没有触发控制器
function setCert(cert) {
location.href = '/Home/CommunitiesLanding/' + cert;
}