2
@using (Ajax.BeginForm("Register","Account",
new AjaxOptions
{
    HttpMethod="POST",
    UpdateTargetId="Done",
    InsertionMode=InsertionMode.Replace,
    OnSuccess="SucceedCreatedNewAcc"
}
)) 
{
@Html.AntiForgeryToken()
@Html.ValidationSummary()
//Fill in the form
 <div id="Done"></div>
}

我的register方法

    [HttpPost]
    [Authorize]
    [ValidateAntiForgeryToken]
    public ActionResult Register(RegisterModel model)
    {
        if (ModelState.IsValid)
        {
            // Attempt to register the user
            try
            {
                WebSecurity.CreateUserAndAccount(model.UserName, model.Password);

                return PartialView(model);
            }
            catch (MembershipCreateUserException e)
            {
                ModelState.AddModelError("", ErrorCodeToString(e.StatusCode));
            }
        }            
        return PartialView();
    }

我的 JS 片段SucceedCreatedNewAcc

function SucceedCreatedNewAcc()
{
    $( "#Done" ).html( "A new account has been created" );
}

我有2个小问题

  1. 我不知道我在SucceedCreatedNewAcc重置我曾经在上面填写的所有表单字段中插入了什么。我想清空所有这些。

  2. 如何检索成功创建的帐户的用户名,以便我可以将其插入SucceedCreatedNewAcc以使我的html文本更有意义,即 $( "#Done" ).html( "A new account @username has been created" );

4

1 回答 1

0
  1. 使用类似document.getElementById('registerForm').reset()$(’#registerForm’)[0].reset();$(':input',’#registerForm’).not(':button,:submit,:reset,:hidden') .val('').removeAttr('checked').removeAttr('selected')

  2. AjaxOptions使用UpdateTargetId="Done"InsertionMode=InsertionMode.Replace。这意味着带有 id 的元素的 htmlDone将被替换为返回控制器Register操作的html。Account因此,您需要的只是从控制器操作返回字符串,如“已创建新帐户@username”$( "#Done" ).html( "A new account has been created" );并从SucceedCreatedNewAcc函数中删除。

于 2013-04-07T21:13:02.057 回答