Using MembershipCreateStatus in MVC
How can I use the MembershipCreateStatus in my controller below to identify errors?
My controller below creates a new user but I would like to catch any errors from CreateStatus and add the error to my modelstate.
I keep getting errors for status below.
[HttpPost]
public ActionResult CreateUser(user UserToCreate)
{
if (ModelState.IsValid)
{
// TODO: If the UserToCreate object is Valid we'll
//Eventually want to save it in a database
MembershipCreateStatus status;
MembershipService newMembershipService = new MembershipService();
MembershipCreateS开发者_Go百科tatus newUser = newMembershipService.CreateUser(UserToCreate.Username, UserToCreate.Password, UserToCreate.Email,out MembershipCreateStatus **status**);
if (newUser == MembershipCreateStatus.Success)
{
return RedirectToAction("Index", "Home");
}
else
{
ModelState.AddModelError(createStatus);
return Redirect("/");
}
}
//Invalid - redisplay form with errors
return View(UserToCreate);
}
You can see in this msdn article how to get the MembershipCreateStatus text. You also have to handle other exceptions that can occure. Since it is a lot of code I suggest to move it in a separate class called MembershipService and just call a method from it.
精彩评论