[ACCEPTED]-ASP.NET MVC: return Redirect and ViewData-asp.net-mvc
You probably want to use the TempData
property, this 2 will be persisted across to the next HTTP 1 request.
Why not handle the login via AJAX instead 3 a full post? You could easily supply the 2 status, a redirect URL, and any error messages 1 via JSON.
public ActionResult Logon( string username, string password )
{
...
// Handle master page login
if (Request.IsAjaxRequest())
{
if (success)
{
return Json( new { Status = true, Url = Url.Action( "Index", "Home" ) } );
}
else
{
return Json( new { Status = false, Message = ... } );
}
}
else // handle login page logon or no javascript
{
if (success)
{
return RedirectToAction( "Index", "Home" );
}
else
{
ViewData["error"] = ...
return View("Logon");
}
}
}
Client-side
$(function() {
$('#loginForm input[type=submit]').click( function() {
$('#loginError').html('');
$.ajax({
url: '<%= Url.Action("Logon","Account") %>',
dataType: 'json',
type: 'post',
data: function() { return $('#loginForm').serialize(); },
success: function(data,status) {
if (data.Status) {
location.href = data.Url;
}
else {
$('#loginError').html( data.Message );
}
}
});
return false;
});
});
Normally for most web sites, when user fail 24 to authenticate (due to password or so), it 23 will go to another page which help the user 22 with (like password retrieve, or ask the 21 user to sign up) which is rare to stay in 20 the very same page. I think you can re-consider 19 that you really need the navigation that 18 you are using.
OK, one solution if you really 17 want to stick to your model, is that you 16 can attach the login error to the URL. For 15 example, http://www.example.com/index.aspx?login_error=1 indicates that error occurs, and 14 you can use BEGIN_REQUEST (or HTTP Module) to 13 capture this, and tell the model state about 12 the error:
ModelState.AddModelError(...);
BTW, add model error is actually 11 a more proper way to inform the view about 10 any error rather than using ViewState (this 9 is similar to throwing exception vs returning 8 an integer about the execution result in 7 old days).
While using AJAX to login (as 6 suggested by tvanfosson) is perfectly achievable 5 and it sometimes excel in user experience, classic 4 full-post is still inreplacable (consider 3 some user will disable javascript, or even 2 on my dump WM6 handset that doesn't support 1 javascript).
I'm confused. Doesn't
return View();
just return the current 7 page back to you?
So in your case, when login 6 fails, set your viewdata and call return 5 View();
i.e.
if (!FailedLogin) {
//Go to success page
}else{
//Add error to View Data or use ModelState to add error
return View();
}
Are you using the [Authorize] decorator? The 4 MVC login process auto prompts with the 3 login page and then returns you to the controller 2 action you were trying to execute. Cuts 1 down on a lot of redirecting.
The following example would hopefully help 7 you out in resolving this issue:
View.aspx
<%= Html.ValidationSummary("Login was unsuccessful. Please correct the errors and try again.") %>
<% using (Html.BeginForm()) { %>
<div>
<fieldset>
<legend>Account Information</legend>
<p>
<label for="username">Username:</label>
<%= Html.TextBox("username") %>
<%= Html.ValidationMessage("username") %>
</p>
<p>
<label for="password">Password:</label>
<%= Html.Password("password") %>
<%= Html.ValidationMessage("password") %>
</p>
<p>
<%= Html.CheckBox("rememberMe") %> <label class="inline" for="rememberMe">Remember me?</label>
</p>
<p>
<input type="submit" value="Log On" />
</p>
</fieldset>
</div>
<% } %>
AccountController.cs
private bool ValidateLogOn(string userName, string password)
{
if (String.IsNullOrEmpty(userName))
{
ModelState.AddModelError("username", "You must specify a username.");
}
if (String.IsNullOrEmpty(password))
{
ModelState.AddModelError("password", "You must specify a password.");
}
if (!MembershipService.ValidateUser(userName, password))
{
ModelState.AddModelError("_FORM", "The username or password provided is incorrect.");
}
return ModelState.IsValid;
}
You won't 6 be able capture the information added in 5 ViewData after Redirect action. so the right 4 approach is to return the same View() and 3 use ModelState for errors as mentioned by 2 "xandy" as well.
Hope this would give u 1 a head start with form validation.
More Related questions
We use cookies to improve the performance of the site. By staying on our site, you agree to the terms of use of cookies.