gpt4 book ai didi

javascript - 为什么通过 Ajax 调用调用我的 Controller 时我的页面没有刷新

转载 作者:行者123 更新时间:2023-12-03 04:38:27 24 4
gpt4 key购买 nike

这是我的标记,用于对 Controller 执行 JavaScript/ajax 回调。我想做的是让这个单击函数激活一个选项卡组件,它会执行此操作,并在 Controller 中执行操作。

<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>

这是 JavaScript

   function setCert(cert){
$.ajax({
url: '/Home/CommunitiesLanding/' + cert,
type: "GET",
traditional: true,
contentType: "application/json",

success: function () {

console.log('success!!');
}
});

最后这是我的 Controller :

 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);
}

最佳答案

如果您的目标是发出 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;
}

关于javascript - 为什么通过 Ajax 调用调用我的 Controller 时我的页面没有刷新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43193906/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com