Reputation: 749
I have simple login form.
Everything works great except one thing. When you enter the page it always shows validation (eg. fields are required), even when no data was POSTed to the controller.
Is there any way to show validation only when there is actually POST request made?
View:
@model LoginViewModel
<form asp-controller="User" asp-action="Login" method="post">
<div asp-validation-summary="ModelOnly" class="text-danger"></div>
<label asp-for="Email"></label>
<input asp-for="Email" />
<span asp-validation-for="Email"></span>
<label asp-for="Password"></label>
<input asp-for="Password" />
<span asp-validation-for="Password"></span>
<button type="submit">Login</button>
</form>
ViewModel:
public class LoginViewModel
{
[Required]
[DataType(DataType.EmailAddress)]
public string Email { get; set; }
[Required]
[DataType(DataType.Password)]
public string Password { get; set; }
}
Action:
[HttpGet]
[HttpPost]
[AllowAnonymous]
public async Task<IActionResult> Login(LoginViewModel model)
{
ClaimsPrincipal userClaims = _userRepository.TryLogin(model);
if (userClaims != null)
{
...
}
return View(model);
}
Upvotes: 3
Views: 1946
Reputation: 9281
As Paul alluded to in the comments, you should remove the [Get]
attribute which will prevent Get requests to the action and instead, create a new controller action responsible for handling get requests that doesn't result in the action's model LoginViewModel
being null.
For instance:
[AllowAnonymous]
public async Task<IActionResult> Login()
{
return View(new LoginViewModel());
}
[HttpPost]
[AllowAnonymous]
public async Task<IActionResult> Login(LoginViewModel model)
{
ClaimsPrincipal userClaims = _userRepository.TryLogin(model);
if (userClaims != null)
{
...
}
return View(model);
}
Now your validation will only trigger as a result of an invalid post model.
Upvotes: 4