如何:(jQuery) 帶有 ASP.NET MVC 2 的模態登錄對話框? (How to: (jQuery) Modal login dialog w/ ASP.NET MVC 2?)


問題描述

如何:(jQuery) 帶有 ASP.NET MVC 2 的模態登錄對話框? (How to: (jQuery) Modal login dialog w/ ASP.NET MVC 2?)

在 ASP.NET MVC 2 中覆蓋內置登錄機制的最佳方法是什麼,以便在用戶需要登錄時彈出模式對話框(或 [Authorize] 標記裝飾控制器操作)?

< p>我已經搜索了一段時間,但沒有什麼真正做到這一點

任何幫助或指導將不勝感激

jQuery 不是先決條件,但我熟悉它


參考解法

方法 1:

It's quite possible and not too hard.

http://weblogs.asp.net/mikebosch/archive/2008/02/15/asp‑net‑mvc‑submitting‑ajax‑form‑with‑jquery.aspx

This is from another post that I can't seem to find any more:

Yes, it's possible. Just submit the login‑form using the method described here by mike bosch and return a json datastructure with the returnUrl if any.

I have created a lightweight LoginResultDTO class that i return as json:

public class LoginResultDTO 
{ 
  public bool Success {get;set;} 
  public string Message {get;set;} 
  public string ReturnUrl {get;set;} 
} 

Here's a script block from my LogOn view:

<script type="text/javascript"> 
        $(document).ready(function() { 
            var form = $($("form")[0]); 
            form.submit(function() { 
                var data = form.serialize(); 
                $.post(form.attr("action"), data, function(result, status) { 
                    if (result.Success && result.ReturnUrl) { 
                            location.href = result.ReturnUrl; 
                    } else { 
                        alert(result.Message); 
                    } 
                }, "json"); 
                return false; 
            }); 
        }); 
</script> 

This will ajax wrap the logon form. Note that this is the simplest implementation of the javascript code possible but it's a place to start.

Then I have modified my LogOn action in the AccountController and in the relevant places put something like this:

if(Request.IsAjaxRequest()) 
{ 
  return Json(new LoginResultDTO{Success=true,Message="Successfully logged in"}); 
}else 
{ 
  return View(); 
} 

So this is an ultralight but rather complete version of how jquery authentication could be done in asp.net mvc.

方法 2:

You can expose a web service in your project that will validate if the user is logged in. On $(document).ready() do an $.ajax() call to the web service. Based on the result you can display the modal popup using the $.dialog() function in the UI library for JQuery.

(by JerroldrboarmanAchilles)

參考文件

  1. How to: (jQuery) Modal login dialog w/ ASP.NET MVC 2? (CC BY‑SA 2.5/3.0/4.0)

#asp.net-mvc-2 #modal-dialog #authorization #jquery






相關問題

使用 c# 將日期時間轉換為日期 (Convert datetime to date using c#)

如何讓 DotNetNuke 6.2 服務框架對 json 數據進行模型綁定 (How can I get DotNetNuke 6.2 Service Framework to modelbind json data)

下拉級聯 MVC 2 (Drop Down Cascading MVC 2)

如何:(jQuery) 帶有 ASP.NET MVC 2 的模態登錄對話框? (How to: (jQuery) Modal login dialog w/ ASP.NET MVC 2?)

如何在 MVC 應用程序的 URL 中使用日期(MM-dd-yyyy 格式)? (How Do I Use a Date (in the MM-dd-yyyy format) in a URL in an MVC Application?)

如何在 asp mvc 2 中進行上傳工作? (How to make upload work in asp mvc 2?)

如何從 ViewData 為 MVC2 Html.HiddenFor 設置一個值 (How to set a value for MVC2 Html.HiddenFor from ViewData)

複雜模型驗證 (Complex model validation)

ASP.NET MVC 2:調用存儲過程,獲取多個結果集 (ASP.NET MVC 2: Calling Stored Procedure, Getting Multiple ResultSets)

JsonResult 相當於 [DataMember(Name="Test")] (JsonResult equivalent to [DataMember(Name="Test")])

ASP.NET MVC 是否適合活動票務網站? (Is ASP.NET MVC a Good Fit for an Event Ticketing Site?)

跨區通話 (Cross area calls)







留言討論