2010-12-20 83 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