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();
}
}
You should definitely use view models, strongly typed views and get rid of any
FormCollectionand magic strings, like this:and then:
The Visual Studio’s default ASP.NET MVC 3 application wizard creates an example of how to do this in the
AccountController.