【问题标题】:MVC + Passing values from one controller to another controllerMVC + 将值从一个控制器传递到另一个控制器
【发布时间】:2012-08-14 10:30:39
【问题描述】:

我是 MVC 的新手,想在这里获得一些关于如何将变量值从一个控制器传递到另一个控制器的建议。基本上,我想要实现的是执行 Facebook 身份验证,成功身份验证后,我应该获取 AccessToken 值和我想将其传递给另一个控制器的完整路径变量值,以便进一步处理新观点。我不确定到目前为止我所做的是否有意义:

我有一个 ActionResult 方法(简化以使其更清晰),如下所示:

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

    string uploadPath = Server.MapPath("~/upload");
    fullpath = uploadPath + "\\ProfilePic.png";

    return null;
}

在我的索引视图中:

<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({
                    type: 'POST',
                    data: postData,
                    success: function () {
                        // process the results from the controller action
                        window.location.href = "Publish";
                    }
                });
            } else {
                alert('User cancelled login');
            }
        }, { scope: 'publish_stream' });
    };

在上面的视图中,我重定向到另一个页面调用“Publish”,它的控制器索引 ActionResult 需要 fbAcess 和 fullpath 变量进一步处理的值。请建议我如何传递这些值。

【问题讨论】:

    标签: c# asp.net-mvc facebook


    【解决方案1】:

    使用重定向:

    [HttpPost]
    public ActionResult Index(string facebookUID, string facebookAccessTok)
    {
        string fbUID = facebookUID;
        string fbAcess = facebookAccessTok;
        var fullpath = "";
    
        string uploadPath = Server.MapPath("~/upload");
        fullpath = uploadPath + "\\ProfilePic.png";
    
        return RedirectToAction("Publish", "TheOtherController", new { fbAccess = fbAccess, fullpath = fullpath });
    }
    
    public class TheOtherController : Controller
    {
        public ActionResult Publish(string fbAccess, string fullpath)
        {
            // Do whatever you want
            //
        }
    }
    

    如果您使用标准表单将数据提交给Index 方法,则此方法有效。如果要保持Ajax发送数据,修改代码如下:

    [HttpPost]
    public ActionResult Index(string facebookUID, string facebookAccessTok)
    {
        string fbUID = facebookUID;
        string fbAcess = facebookAccessTok;
        var fullpath = "";
    
        string uploadPath = Server.MapPath("~/upload");
        fullpath = uploadPath + "\\ProfilePic.png";
    
        var redirectUrl = new UrlHelper(Request.RequestContext).Action("Publish", new { fbAcess = fbAcess, fullpath = fullpath });
        return Json(new { Url = redirectUrl });
    }
    

    在您的客户端代码中:

    $.ajax({ type: 'POST',
             data: postData,
             dataType: 'json',
             success: function (response) {               
                 window.location.href = response.Url;
             }
    });
    

    【讨论】:

    • 再问一个问题,如果我使用 return Redirect(redirectUrl); 有什么区别?而不是 return Json(new { Url = redirectUrl });因为后者给了我“Windows 资源管理器已停止工作”页面加载错误。谢谢。
    • 抱歉耽搁了。如果您发现错误的确切来源,是否可以在 IE 的开发人员工具中进行调查?
    【解决方案2】:

    认证成功后调用如下方法

    return RedirectToAction("ActionName", "Controller", new {variable1 = value1, variable2 = value2/*...etc*/});
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-11-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-08-16
      • 1970-01-01
      相关资源
      最近更新 更多