问题描述:

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

}

My register method

 [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" );

}

I have 2 little issues

  1. I don't know what I insert in the SucceedCreatedNewAcc to reset all form field I once filled in above. I would like to empty all of them.

  2. How can I retrieve the successfully created account's username so that I can insert it into SucceedCreatedNewAcc to make my html text more meaningful i.e $( "#Done" ).html( "A new account @username has been created" );

网友答案:
  1. Use something like document.getElementById('registerForm').reset() or $(’#registerForm’)[0].reset(); or $(':input',’#registerForm’).not(':button,:submit,:reset,:hidden') .val('').removeAttr('checked').removeAttr('selected')

  2. You use AjaxOptions with UpdateTargetId="Done" and InsertionMode=InsertionMode.Replace. That mean that html of element with id Done will be replaced with html that returns Register action of Account controller. So all that you need is return string like "A new account @username has been created" from controller action and remove $( "#Done" ).html( "A new account has been created" ); from SucceedCreatedNewAcc function.

相关阅读:
Top