2012-08-14 179 views
1

我是MVC的新手,希望在此討論如何將變量值從一個控制器傳遞到另一個控制器。基本上,我想要實現的是執行Facebook身份驗證,並且在成功身份驗證後,我應該得到值和完整路徑變量值,我想將它傳遞給另一個控制器以便用新視圖進一步處理。我不知道如果我做了什麼至今道理:MVC +將值從一個控制器傳遞到另一個控制器

我有一個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' }); 
    }; 

在上面的視圖中,我做了重定向到另一個頁面調用「發佈」,其控制器索引ActionResult要求fbAcess全路徑用於進一步處理的變量值。請諮詢我如何傳遞價值觀。

回答

3

使用重定向:

[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; 
     } 
}); 
+0

感謝,這有助於! – k80sg 2012-08-15 02:05:48

+0

還有一個問題,如果我使用return Redirect(redirectUrl),有什麼區別;而不是返回Json(new {Url = redirectUrl});因爲後者給我一個「Windows資源管理器已停止工作」頁面加載錯誤。謝謝。 – k80sg 2012-08-15 03:43:10

+0

抱歉,延遲。如果您發現錯誤的確切來源,您可以在IE的開發人員工具中進行調查嗎? – twoflower 2012-08-17 13:04:51

1

身份驗證成功,請撥打以下方法

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