【发布时间】:2017-04-03 20:43:40
【问题描述】:
这是执行控制器的 JavaScript/ajax 回调的标记。我想要做的是让这个点击功能激活一个选项卡组件,它会这样做,并在控制器中执行操作。
<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!!');
}
});
最后是我的控制器:
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);
}
【问题讨论】:
-
你使用的是什么 MVC 框架?
-
为什么要刷新页面?您正在进行 AJAX 调用(不涉及浏览器在任何地方导航用户)并且仅在成功时执行
console.log。 -
这是使用 .NET 框架
-
我意识到我在进行 Ajax 调用,也许您可以引导我朝着正确的方向前进。我想通过再次执行控制器来刷新页面。
-
如果你想从控制器动态加载标签内容,考虑使用局部视图。要了解这个想法,请查看this answer。