Snowing Code

Personal notes on software development


Redirecting in Fubu

(Publish date: 05/03/2010)

This is a very straight forward post, just a reminder of how do you redirect and transfer in FubuMVC.

Using the FubuDinner example, we have a registration page, and its GET action:

public RegisterModel Query(RegisterModel input)
           {
           input.PasswordLength = _settings.MinPasswordLength;
          return input;
          }

Now, in our POST method we’d like to log on the user and redirect to our Home page if the registration went ok (validated and persisted); however, if the registration wasn’t validated, we’d like to get back to our registration page, display the error messages and keep the data the user has entered.

This is how the POST method would look like than:

public FubuContinuation Command(RegisterForm input)
           {
           var errors = _validator.Validate(input);
          
           //TODO: need unique username validation
          
           if( input.Nerd.Password != input.ConfirmPassword )
           {
           errors.RegisterFailure("ConfirmPassword", "Confirm password must match password exactly.");
           }
          
           if( errors.IsValid())
           {
           _repository.Save(input.Nerd);
           _authContext.ThisUserHasBeenAuthenticated(input.Nerd.Username, false);
           return FubuContinuation.RedirectTo(new HomeModel());
           }
          
           return FubuContinuation.TransferTo(new RegisterModel { Errors = errors, Nerd = input.Nerd, ConfirmPassword = input.ConfirmPassword });
          }

Simples!

Tags: fubu fubumvc mvc
blog comments powered by Disqus