2012-03-23 28 views
1

如何更改此方法來檢查登錄名和密碼,以及它不刷新日誌表單所在的整個頁面(日誌表單位於jQuery對話框中)。此方法不應將我重定向到/ Home/Index或刷新整個頁面。應該只是登錄我,然後jQuery腳本應該關閉對話窗口。不返回任何視圖的控制器方法

的AccountController:

[HttpPost] 
    public ActionResult LogOnDialogForm(LogOnModel model, string returnUrl) 
    { 
     if (ModelState.IsValid) 
     { 
      if (Membership.ValidateUser(model.UserName, model.Password)) 
      { 
       FormsAuthentication.SetAuthCookie(model.UserName, model.RememberMe); 
       if (Url.IsLocalUrl(returnUrl) && returnUrl.Length > 1 && returnUrl.StartsWith("/") 
        && !returnUrl.StartsWith("//") && !returnUrl.StartsWith("/\\")) 
       { 
        return Redirect(returnUrl); 
       } 
       else 
       { 
        return RedirectToAction("Index", "Home"); 
       } 
      } 
      else 
      { 
       ModelState.AddModelError("", "The user name or password provided is incorrect."); 
      } 
     } 

     // If we got this far, something failed, redisplay form 
     return View("LogOn"); 
    } 

從視圖jQuery腳本(我在那裏登錄按鈕,打開對話框):

<script> 
$(function() { 
    $("#dialog:ui-dialog").dialog("destroy"); 

    var name = $("#name"), 
     password = $("#password"), 
     allFields = $([]).add(name).add(password), 
     tips = $(".validateTips"); 

    function updateTips(t) { 
     tips 
      .text(t) 
      .addClass("ui-state-highlight"); 
     setTimeout(function() { 
      tips.removeClass("ui-state-highlight", 500); 
     }, 500); 
    } 

    function checkLength(o, n, min, max) { 
     if (o.val().length > max || o.val().length < min) { 
      o.addClass("ui-state-error"); 
      updateTips("Length of " + n + " must be between " + 
       min + " and " + max + "."); 
      return false; 
     } else { 
      return true; 
     } 
    } 

    $("#dialog-form").dialog({ 
     autoOpen: false, 
     height: 380, 
     width: 350, 
     modal: true, 
     buttons: { 
      "Login": function() { 
       var form = $('form', this); 
       $(form).submit(); 
       $(this).dialog("close"); 
      }, 
      Cancel: function() { 
       $(this).dialog("close"); 
      } 
     }, 
     close: function() { 
      allFields.val("").removeClass("ui-state-error"); 
     } 
    }); 

    $("#login") 
     .button() 
     .click(function() { 
      $("#dialog-form").dialog("open"); 
     }); 
}); 
</script> 

查看其中包含的形式體:

@model BlogNet.Models.LogOnModel 

<p> 
    @Html.ActionLink("Register", "Register") if you don't have an account. 
</p> 

<script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script> 
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script> 

@Html.ValidationSummary(true, "Login was unsuccessful. Please correct the errors and try again.") 

@using (Html.BeginForm("LogOnDialogForm", "Account", FormMethod.Post, new { @class = "dialogForm"})) 
{ 
    <div> 
     <fieldset> 
      <div class="editor-label"> 
       @Html.LabelFor(m => m.UserName) 
      </div> 
      <div class="editor-field"> 
       @Html.TextBoxFor(m => m.UserName) 
       @Html.ValidationMessageFor(m => m.UserName) 
      </div> 

      <div class="editor-label"> 
       @Html.LabelFor(m => m.Password) 
      </div> 
      <div class="editor-field"> 
       @Html.PasswordFor(m => m.Password) 
       @Html.ValidationMessageFor(m => m.Password) 
      </div> 

      <div class="editor-label"> 
       @Html.CheckBoxFor(m => m.RememberMe) 
       @Html.LabelFor(m => m.RememberMe) 
      </div> 
     </fieldset> 
    </div> 
} 

回答

1

您需要AJAXify您的登錄表單。現在它是一個標準的HTML <form>,它在提交時將輸入字段POST到服務器並刷新整個頁面。

所以:

"Login": function() { 
    var dialog = this; 
    var form = $('form', dialog); 
    $.ajax({ 
     url: form.attr('action'), 
     type: form.attr('method'), 
     data: form.serialize(), 
     success: function(result) { 
      if (result.success) { 
       // authentication was successful 
       // Here you can close the dialog, redirect, refresh 
       // some part of the DOM, whatever you want 
       $(dialog).dialog("close"); 
      } else { 
       // an error occurred => we refresh the dialog: 
       $('#dialog-form').html(result); 
      } 
     } 
    }); 
} 

,你也應該修改您的控制器操作,以便在認證成功的情況下,它只是返回的不是重定向JSON:

if (Membership.ValidateUser(model.UserName, model.Password)) 
{ 
    FormsAuthentication.SetAuthCookie(model.UserName, model.RememberMe); 
    if (Url.IsLocalUrl(returnUrl) && returnUrl.Length > 1 && returnUrl.StartsWith("/") 
     && !returnUrl.StartsWith("//") && !returnUrl.StartsWith("/\\")) 
    { 
     return Json(new { success = true, returnUrl = returnUrl }); 
    } 
    else 
    { 
     return Json(new { success = true, returnUrl = Url.Action("Index", "Home") }); 
    } 
} 
+0

類似於礦山,但不會由於該方法中沒有實際失敗,因此操作方法的失敗仍然會「成功」 – hunter 2012-03-23 14:49:51

+0

更新後更清晰 – hunter 2012-03-23 14:51:12

+0

@ hunter,絕對是。只需要區分認證成功的情況和例如用戶輸入錯誤密碼的情況。在這第二種情況下,您可能希望在對話框中向他顯示​​一些錯誤消息,通知他這件事。 – 2012-03-23 14:52:12

0

嘗試改變的Login部分您的代碼:

"Login": function() { 
    var form = $("form", this); 
    $.post(form.attr("action"), form.serialize(), function(data) {    
     if ($(data).find(".validation-summary").length > 0) 
     { 
      $("#dialog-form").html(data); 
     } 
     else { $(this).dialog("close"); } 
    }); 
}, 
+0

當成功返回模型時看起來有點沉重,但我喜歡這樣一個事實,即它允許您在對話框中重複使用相同的動作/視圖 – hunter 2012-03-23 14:56:49

相關問題