Ajax's表单创建新用户

本文关键字:新用户 用户 创建 表单 Ajax | 更新日期: 2023-09-27 18:03:28

@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();
    }

My JS piece of SucceedCreatedNewAcc

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

我有两个小问题

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

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

Ajax's表单创建新用户

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

  2. 您使用AjaxOptionsUpdateTargetId="Done"InsertionMode=InsertionMode.Replace。这意味着id为Done的元素的html将被返回Account控制器的Register动作的html所取代。所以所有你需要的是返回字符串像"一个新的帐户@username已创建"从控制器动作和删除$( "#Done" ).html( "A new account has been created" );SucceedCreatedNewAcc功能。