简体   繁体   English

为什么通过Ajax调用控制器时页面没有刷新

[英]Why isn't my page doing a refresh when calling my controller though an Ajax call

Here is my markup that executes the JavaScript/ajax callback to the controller. 这是我执行控制器的JavaScript / ajax回调的标记。 What I am trying to do is have this click function activate a tab component, which it does, and execute the action in the Controller. 我想做的是让此单击功能激活一个选项卡组件,并在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>

Here is the JavaScript 这是JavaScript

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

            success: function () {

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

and lastly here is my 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);
    }

If your goal is to make the AJAX request to initialize some data and then redirect the user to the newly initialized page once the server is done processing, you can update your AJAX function to redirect on success: 如果您的目标是发出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;
        }
    });
}

It might make more sense to just redirect the user directly though, if you don't need to initialize data and wait for it to complete beforehand: 但是,如果您不需要初始化数据并事先等待数据完成,则直接重定向用户可能更有意义:

function setCert(cert) {
   location.href = '/Home/CommunitiesLanding/' + cert;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM