views:

161

answers:

1

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();
            MembershipCreateStatus 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);
    }
A: 

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.

Branislav Abadjimarinov
I've created a MembershipService class with all the Membership Provider methods but I still don't see how I can use the MembershipCreateStatus?
Jemes