2010-12-20 77 views
1

嗨,有人可以告訴我這個問題,請及時解決。更新在asp.net中使用ajax mvc

我想使用ajax更新頁面的一部分,我正在使用新的mvc項目附帶的基本代碼。

登錄頁有這樣的:

<span id="error"/> 
@using (Ajax.BeginForm("LogOn", "Account", new AjaxOptions { UpdateTargetId="error"})) { 
    <div> 
     <fieldset> 
      <legend>Account Information</legend> 

      <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> 

      <p> 
        <input type="submit" value="Log On" /> 
      </p> 
     </fieldset> 
    </div> 
} 

和控制器,像這樣:

 [HttpPost] 
     public string LogOn(LogOnModel model, string returnUrl) 
     { 

      if (ModelState.IsValid) 
      { 
       if (MembershipService.ValidateUser(model.UserName, model.Password)) 
       { 
        FormsService.SignIn(model.UserName, model.RememberMe); 
        if (Url.IsLocalUrl(returnUrl)) 
        { 
         Redirect(returnUrl); 
        } 
        else 
        { 
         RedirectToAction("Index", "Home"); 
        } 
       } 
       else 
       { 
        ModelState.AddModelError("", "The user name or password provided is incorrect."); 
       } 
      } 

      return "The user name or password provided is incorrect."; 
     } 

基本上我做的是內嵌登錄表單到一個模式彈出。 如果輸入的用戶憑證失敗我希望在模式彈出窗口中顯示錯誤,而不是進入另一頁面。

上面的代碼只是創建一個空白頁面,文本爲「提供的用戶名或密碼不正確」。我需要它在模式對話框(jQuery)中顯示。

+0

你能告訴我們LogonModel?我假設它只是用戶名,密碼和RememberMe,但有沒有使用任何DataAnnotations?可能的話,如果你正在檢查ModelState。 – 2010-12-20 17:15:18

+0

logonModel是一個帶有新的mvc項目的包裝盒 – raklos 2010-12-20 18:59:50

回答

0

那麼,最明顯的問題是,你有腳本添加到您試圖做到這一點的網頁?其次,如果你發佈到HTTPS(你是吧?),你的腳本也必須是HTTPS,否則你會得到一個安全錯誤。

+0

添加了腳本,並使用新項目附帶的標準開箱即用登錄代碼。 – raklos 2010-12-20 19:01:23