0

我有一个 HttpPost 'Index' ActionResult,它重定向到另一个 ActionResult 调用'Publish'。问题是当我在这一行设置断点时:

 if (accTok != null && fullImgPath != null)

单击我的 Ajax ActionLink 时它将通过两次,我只能在第二次通过时获得值,因此我的“发布”控制器将始终返回我的“空白”视图。我知道这可能与 Ajax 帖子有关,但我不确定如何纠正这个问题,以便我可以在第一次通过时获得我的“accTok”和“fullImgPath”值,以便它显示我的“BlurredPhoto”部分看法。请协助。谢谢。

我发现问题是由于我的@Ajax.ActionLink 首先触发并在我的 [HttpPost] 方法之前重定向到“发布”。

public ActionResult Index()
        {
             return View(); 
        }

    [HttpPost]
    public ActionResult Index(string facebookUID, string facebookAccessTok)
    {
        string fbUID = facebookUID;
        string fullPath = "";

        if (fbUID != null)
        {           
                // Request fb profile pic
                var rawImg = new Bitmap(ImageHelper.requestBitmapImage(fbUID));
                var processblurredImg = new Bitmap(rawImg);

                var gb = new GaussianBlur();

                for (int i = 0; i < 8; i++)
                {
                    gb.ApplyInPlace(processblurredImg);
                }

                // Download it to local drive / server
                string uploadPath = Server.MapPath("~/upload");
                fullPath = uploadPath + "\\ProfilePic.png";

                if (!Directory.Exists(uploadPath))
                {
                    Directory.CreateDirectory(uploadPath);
                }
                if (uploadPath != null)
                    ImageHelper.savePng(fullPath, processblurredImg, 500L);

        }

        return RedirectToAction("Publish", new { accTok = facebookAccessTok, fullImgPath = fullPath });
    }

public PartialViewResult Publish( string accTok, string fullImgPath)
{
    if (accTok != null && fullImgPath != null)
    {
         UploadPhoto(accTok, fullImgPath);
        // PostToWall(accTok, fullPath);
         return PartialView("BlurredPhoto");
    }
    return PartialView("Blank");
}

我的索引视图:

       @using Specsavers_Fred_Hollow.Helpers

@{

    ViewBag.Title = "Home Page";
    //Layout = "~/Views/Shared/_Layout.cshtml";
}

<script type="text/javascript">
    var uid = 0;
    var accesstoken = '';

    function grantPermission() {
        window.FB.login(function (response) {
            if (response.authResponse) {
                uid = response.authResponse.userID;
                accesstoken = response.authResponse.accessToken;
                var postData = { facebookUID: uid, facebookAccessTok: accesstoken };
                $.ajax({
                    url: '@Url.Action("Index","Tab")',
                    type: 'POST',
                    data: postData,
                    dataType: 'json',
                    success: function (response) {
                        // process the results from the controller action
                        // window.location.href = response.Url;
                    }
                });
            } else {
                console.log('User cancelled login or did not fully authorize.');
                alert('User cancelled login');
            }
        }, { scope: 'publish_stream' });
    };

// Init the SDK upon load
    window.fbAsyncInit = function() {
        FB.init({
            appId: '@FacebookHelper.FacebookAppId', // App ID
            channelUrl: '//WWW.YOUR_DOMAIN.COM/channel.html', // Channel File
            status: true, // check login status
            cookie: true, // enable cookies to allow the server to access the session
            xfbml: true  // parse XFBML
        });

        // Load the SDK Asynchronously
        (function(d) {
            var js, id = 'facebook-jssdk', ref = d.getElementsByTagName('script')[0];
            if (d.getElementById(id)) {
                return;
            }
            js = d.createElement('script');
            js.id = id;
            js.async = true;
            js.src = "//connect.facebook.net/en_US/all.js";
            ref.parentNode.insertBefore(js, ref);
        }(document));


        // listen for and handle auth.statusChange events
        FB.Event.subscribe('auth.statusChange', function(response) {
            if (response.authResponse) {
                // user has auth'd your app and is logged into Facebook
                FB.api('/me', function(me) {
                    if (me.name) {
                        document.getElementById('auth-displayname').innerHTML = me.name;
                    }
                });
                document.getElementById('auth-loggedout').style.display = 'none';
                document.getElementById('auth-loggedin').style.display = 'block';
            } else {
                // user has not auth'd your app, or is not logged into Facebook
                document.getElementById('auth-loggedout').style.display = 'block';
                document.getElementById('auth-loggedin').style.display = 'none';
            }
        });
        $("#auth-loginlink").click(function() { grantPermission(); });
    };

</script>

<html>
    <head>
        <title>Facebook Login Authentication Example</title>
        <script type="text/javascript" src="http://code.jquery.com/jquery-latest.js"> </script>

    </head>
    <body>

        <h1>Facebook Login Authentication</h1>
        <br/>

        <div id="auth-status">
            @*<a href="#" id="auth-loginlink">Login</a><br/>*@
            @*<div class="fb-login-button" id="auth-loginlink"></div>*@
        </div>

        @Ajax.ActionLink("Proceed", "Publish", "Tab", 
        new { id = "auth-loginlink" },
        new AjaxOptions{UpdateTargetId = "DynamicContainer",
                        InsertionMode=InsertionMode.Replace,
                        HttpMethod="POST",
                        OnSuccess = "grantPermission()" 
        })

        <br/><br/>

        <div id="DynamicContainer" style="border: 1px solid #C0C0C0; padding: 1px; width: 500px; height: 400px"></div>

    </body>
</html>
4

0 回答 0