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

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

点击我的Ajax ActionLink后它会经过两次,我只能在第二遍传递值,因此我的'Publish'控制器将始终返回我的'Blank'视图 . 我知道这可能与Ajax帖子有关,但我不知道如何纠正这个问题,以便我可以在第一遍获得我的'accTok'和'fullImgPath'值,以便它显示我的'BlurredPhoto'部分视图 . 请帮忙 . 谢谢 .

I found out that the issue is due to my @Ajax.ActionLink which fires first and redirect to 'Publish' before my [HttpPost] method.

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>
        
<div id="auth-status"> @*<a href="#" id="auth-loginlink">Login</a>
*@ @*<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()" })

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