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);
}