2017-04-03 55 views
0

這是我的標記,它執行JavaScript/ajax回調到控制器。我試圖做的是讓這個點擊功能激活一個選項卡組件,然後在Controller中執行這個動作。爲什麼我的頁面在通過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); 
    } 
+0

什麼MVC框架您使用的? –

+0

你爲什麼期待頁面刷新?您正在進行AJAX調用(不涉及瀏覽器在任何地方導航用戶),並且只在成功時執行'console.log'。 –

+0

這是使用.NET框架 –

回答

1

如果你的目標是使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; 
} 
+0

我也想知道這些相同的觀點,如果試圖在結果頁面的某些部分做某種SPA ... –

+0

那是行不通的。恐怕。它沒有觸發控制器 –

+0

,但感謝您的幫助 –

相關問題