Controller:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Projectinnovation.Viewmodels;
using Projectinnovation.Models;
using Projetodeinovacao.Classes;
namespace Projectinnovating.Controllers
{
public class Homecontroller : Controller
{
CadastroBLL bll = new CadastroBLL();
[HttpGet]
public ActionResult Login()
{
return View();
}
// instancia e manda valores para variaveis
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Login(Cadastro model)
{
if (ModelState.IsValid)
{
ViewData["Nome"] = model.Nome;
ViewData["Email"] = model.Email;
ViewData["Senha"] = model.Senha;
bll.cadastrar(model);
ModelState.Clear();
return View(model);
}
else
{
return View("Login");
}
}
public ActionResult Index()
{
ViewBag.Title = "Amorelli - Home";
return View();
}
public ActionResult Carrinho()
{
ViewBag.Title = "Amorelli - Carrinho";
return View();
}
}
}
Model:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
Using system.ComponentModel.Dataannotations;
namespace Projetodeinovacao.Models
{
public class Cadastro
{
private string name;
[Required (Errormessage = "Required to enter name")]
public string Name
{
get { Return name; }
set { name = value; }
}
private string email;
[Required(Errormessage = "Required to inform E-mail")]
[Regularexpression(".+ @.+ ..+", Errormessage = "E-mail is not valid")]
public string Email
{
get { Return email; }
set { email = value; }
}
private string password;
[Required(Errormessage = "Required to enter a password")]
[Datatype(Datatype.Password)]
[Stringlength(20, Minimumlength = 8)]
public string Password
{
get { Return password; }
set { password = value; }
}
}
}
View:
@model Projectinnovation.Models.Registration
@{
Viewbag.Title = "Amorelli - Login";
}
/*PARA VALIDAÇÃO DOS HELPERS*/
.field-validation-error {
color: #f00;
}
.field-validation-valid {
display: none;
}
.input-validation-error {
border: 1px solid #f00;
background-color: #fee;
}
.validation-summary-errors {
font-weight: bold;
color: #b94a48;
}
.validation-summary-valid {
display: none;
}
Sign in to your account
Keep me connected
Login
OR
Register!
@using (Html.Beginform("Login", "Home"))
{
@Html.Antiforgerytoken();
@Html.Textboxfor(Model => Model.Name, new { id = "Name", placeholder = "Full Name" })
@Html.Textboxfor(Model => Model.Email, new { id = "Email1", placeholder = "Email" })
@Html.Textboxfor(Model => Model.Password, new { id = "Password1", placeholder = "Password", type = "password" })
Register
@Html.Validationsummary(false, "Please correct the errors below:")
}
(login form still being created)
You in the controller, when you don’t accept the validation, are returning the same model to the View, and placing the Razor tags so you can display the validation messages. Sometimes it might be CSS too that might not be showing tags.
– Grupo CDS Informática
I removed all css code and tested it and it didn’t work. Already my controller meets the validation I return my view(), and if it does not answer I return the index, but it does not matter if I change it does not appear the messages only validates
– Leo Lima
puts your controller code there, model and view
– Eduardo Sampaio