2013-06-12 12 views
1

有人可以在jQuery對話框中向我建議什麼是乾淨和正確的呈現方式,什麼是MVC 4 RedirectResult?在jQuery對話框中使用Facebook登錄 - 在jQuery對話框中使用MVC 4 ActionResult

我有一個Facebook插件返回一個「RedirectResult」,以便在登錄過程中用戶在Facebook登錄頁面重定向,然後返回到網站。 我想在jQuery對話框中加載登錄頁面,以便不離開網站頁面。

在我的控制,我有:

if (result.Result != null) return result.Result; 

    return HttpContext.Request.IsAuthenticated ? new RedirectResult(!string.IsNullOrEmpty(returnUrl) ? returnUrl : "~/") : new RedirectResult(Url.LogOn(returnUrl)); 

「result.Result」 是RedirectResult類型,在這裏我有一個包含重定向的URL。

在與jQuery的對話視圖,我想加載登錄Facebook頁面:

@using Nop.Web.Framework.UI 
    @{ 
    Layout = ""; 
    Html.AddCssFileParts("./Plugins/ExternalAuth.Facebook/Content/facebookstyles.css"); 
    } 

<fieldset class="facebook-login-block"> 
    <legend>@T("Plugins.ExternalAuth.Facebook.Login")</legend> 

    <a href="#" class="facebook-btn" id="facebook-btn">&nbsp;</a> 
</fieldset> 

    <script type="text/javascript"> 
      $(document).ready(function() { 
       $("a#facebook-btn").click(function (e) { 
        e.preventDefault(); 
        $('#dialog').dialog({ 
         resizable: true, 
         height: 250, 
         width: 400, 
         modal: true, 
         open: function (event, ui) { 
          $('#dialog').load("@Url.RouteUrl("Plugin.ExternalAuth.Facebook.Login", new { ReturnUrl = HttpContext.Current.Request.QueryString["ReturnUrl"] })");    
         } 
        }); 


      }); 
     }); 
    </script> 

    <div id="dialog" style="display:none"></div> 

這是原始呼叫:

<a href="@Url.RouteUrl("Plugin.ExternalAuth.Facebook.Login", new { ReturnUrl = HttpContext.Current.Request.QueryString["ReturnUrl"] })" class="facebook-btn"> 
    &nbsp;</a> 

我試圖把它移到負載功能,但這並沒有產生任何結果。

任何建議將非常感激。 謝謝

回答

0

我認爲我自己找到了一個可能的解決方案,我不知道這是否是最好的和乾淨的方式,但工程。關注細節。

我改變了我的控制器這樣的:

 if (result.Result != null) // return result.Result; 
     { 
      RedirectResult RR = (RedirectResult)result.Result; 
      return Content(RR.Url); 
     } 

     return HttpContext.Request.IsAuthenticated ? new RedirectResult(!string.IsNullOrEmpty(returnUrl) ? returnUrl : "~/") : new RedirectResult(Url.LogOn(returnUrl)); 

和我的觀點這樣:

<fieldset class="facebook-login-block"> 
    <legend>@T("Plugins.ExternalAuth.Facebook.Login")</legend> 

    @*<a href="@Url.RouteUrl("Plugin.ExternalAuth.Facebook.Login", new { ReturnUrl = HttpContext.Current.Request.QueryString["ReturnUrl"] })" class="facebook-btn"> 
     &nbsp;</a>*@ 
    <a href="#" class="facebook-btn" id="facebook-btn">&nbsp;</a> 
</fieldset> 

<script type="text/javascript"> 
     $(document).ready(function() { 
      $("a#facebook-btn").click(function (e) { 
       e.preventDefault(); 

       $.ajax({ 
        type: "GET", 
        async: false, 
        url: "@Url.RouteUrl("Plugin.ExternalAuth.Facebook.Login", new { ReturnUrl = HttpContext.Current.Request.QueryString["ReturnUrl"] })", 
        contentType: "application/json; charset=utf-8", 
        error: function() { return true; }, 
        success: function (data) { 
         var $dialog = $('#dialog') 
             .html('<iframe style="border: 0px; " src="' + data + '" width="100%" height="100%"></iframe>') 
             .dialog({ 
             resizable: true, 
             height: 250, 
             width: 400, 
             modal: true 
             }); 

         $dialog.dialog('open'); 
        } 
       }); 
     }); 
    }); 
</script> 

<div id="dialog" style="display:none"></div> 

任何其他解決方案是值得歡迎的。

+0

我的第一種方法是在身份驗證過程中正常工作。不幸的是,我發現Facebook不喜歡iframe,所以我不得不使用window.open而不是jQuery對話框。如果別人有更好的解決方案,歡迎他讓我知道。 – Maverjk