I'm creating a login screen in ASP.NET.Core MVC, and I'm using validations via Data.Annotations , however they are not being applied correctly on the screen.
Following codes:
View:
@model PCI.EV.Models.Clientes
<script src='https://www.google.com/recaptcha/api.js'></script>
<h2>ACESSE SUA CONTA</h2>
<form asp-controller="Accounts" asp-action="Login" method="post">
<div class="container">
<div class="form-group">
<label class="control-label" asp-for="CliCodigo">ID</label>
<input type="text" asp-for="CliCodigo" class="form-control" />
<span asp-validation-for="CliCodigo" class="text-danger"></span>
</div>
<div class="form-group">
<label class="control-label" asp-for="CliSenhaWeb">SENHA</label>
<br />
<input type="text" asp-for="CliSenhaWeb" class="form-control" />
<span asp-validation-for="CliSenhaWeb" class="text-danger"></span>
</div>
<br />
<div class="g-recaptcha" data-sitekey="6LcqFg8UAAAAADUK7coDHgoeTXEdJ9JQICdCyVcP">
</div>
<br />
<input type="submit" asp-action="FazerLogin" class="btn btn-primary" value="Login" />
</div>
</form>
Controller:
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using PCI.EV.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace PCI.EV.Controllers
{
public class AccountsController : Controller
{
private IConfiguration _config;
public AccountsController(IConfiguration configuration)
{
_config = configuration;
}
public IActionResult Login()
{
return View();
}
[HttpPost]
public IActionResult FazerLogin(Clientes cli)
{
if (ModelState.IsValid)
{
}
return View("Login", cli);
}
}
}
Model
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Threading.Tasks;
namespace PCI.EV.Models
{
public class Clientes
{
[Required(ErrorMessage = "Campo obrigatório")]
public int CliCodigo { get; set; }
[Required(ErrorMessage = "Campo obrigatório")]
[DataType(DataType.Password)]
[StringLength(10, MinimumLength = 4)]
public string CliSenhaWeb { get; set; }
}
}
Screen:
[! [insert image description here] [2]] [2]