Sparkle
Sparkle

Reputation: 2469

Return View with form data

I have the below controller. I return the view if a error occurs but the form data is lost. Would anyone have a idea how I could return the form data with the view?

[AcceptVerbs(HttpVerbs.Post)]
    public ActionResult Register(FormCollection collection)
    {
        string usrname = collection["UserName"];
        string email = collection["Email"];
        string password = collection["Password"];
        string serial = collection["Serial"];
        ViewData["PasswordLength"] = MembershipService.MinPasswordLength;
        // In a real app, actually register the user now
        if (ValidateRegistration(usrname, email, password, password))
        {
            // Attempt to register the user
            MembershipCreateStatus createStatus = MembershipService.CreateUser(usrname, password, email, serial);

            if (createStatus == MembershipCreateStatus.Success)
            {
                //TODO userinformation

                datacontext.SaveChanges();
                FormsAuth.SignIn(collection["UserName"], false /* createPersistentCookie */);
                return RedirectToAction("Index", "Home");
            }
            else
            {
                ModelState.AddModelError("_FORM", ErrorCodeToString(createStatus));

                //I would like to return the view with the form data

                return View();
            }
        }

Upvotes: 0

Views: 1724

Answers (2)

Darin Dimitrov
Darin Dimitrov

Reputation: 1038720

You should definitely use view models, strongly typed views and get rid of any FormCollection and magic strings, like this:

public class RegisterUserViewModel
{
    public string UserName { get; set; }
    public string Email { get; set; }
    public string Password { get; set; }
    public string Serial { get; set; }
    public int PasswordLength { get; set; }
}

and then:

[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Register(RegisterUserViewModel model)
{
    model.PasswordLength = MembershipService.MinPasswordLength;
    // In a real app, actually register the user now
    if (ValidateRegistration(model.UserName, model.Email, model.Password, model.Password))
    {
        // Attempt to register the user
        MembershipCreateStatus createStatus = MembershipService.CreateUser(model.UserName, model.Password, model.Email, model.Serial);
        if (createStatus == MembershipCreateStatus.Success)
        {
            //TODO userinformation
            datacontext.SaveChanges();
            FormsAuth.SignIn(model.UserName, false /* createPersistentCookie */);
            return RedirectToAction("Index", "Home");
        }
        else
        {
            ModelState.AddModelError("_FORM", ErrorCodeToString(createStatus));
            return View(model);
        }
    }

    ...
}

The Visual Studio's default ASP.NET MVC 3 application wizard creates an example of how to do this in the AccountController.

Upvotes: 2

Jakub Konecki
Jakub Konecki

Reputation: 46008

First of all, I would recommend using PRG pattern (do not return view from our POST action)

You will need to store ModelState in temp data, but you can do it easily with action filter attributes - see point 13. in this blog post.

Upvotes: 0

Related Questions