Using the MVC project template in VS2008 (out of the box) I noticed the following:
Here is how the Register.aspx form is specified.
<% using (Html.BeginForm()) { %>
Selecting the Register button without supplying any account info shows this. Account creation was unsuccessful. Please correct the errors and try again.
• You must specify a username.
• You must specify an email address.
• You must specify a password of 6 or more characters.I changed the Register.aspx form to this.
<% using (Ajax.BeginForm("Register", new AjaxOptions { HttpMethod = "Post" })) { %>
Selecting the Register button without supplying an account info shows no errors.
Question: How do I get the error text to display when using Ajax.BeginForm ?
To successfully apply ajax form submission you will have to modify the Register action and the views. By default, in case of error, this action just returns the associated register.aspx view. The first step would be to put the validation summary inside a partial user control:
ValidationSummary.ascx:
Then you include this partial inside the Register.aspx view:
And finally you modify the Register action:
In case the registration succeeds by default the Register action just redirects to Home/Index. You will have to modify this bit as well, because as you are performing an ajax request the redirection won't work. The same way you could test if you are invoking the action asynchronously and return some text that will indicate that the registration was successful. This text will be displayed inside the same
div
as the validation errors.UPDATE:
In order to achieve this you need to put the contents of your form inside a partial view:
And in your register action render the correct partial: