Data validation ASP.NET MVC works but does not display message

Asked

Viewed 722 times

0

Good night! I am having a problem in my project where I am trying to validate with date Annotations and is working, only register at the bank if meet the validations, but the problem is that the messages that I put to display next to the data Annotations do not work and I have no idea why this is happening, I have looked at several codes and is identical but my validates but does not display the message. Somebody please help me!

  • 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.

  • 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

  • puts your controller code there, model and view

2 answers

1

I was able to find the problem, in @Html.Validationsummary() I put @Html.Validationsummary(true), it has to be @Html.Validationsummary(), thanks for the help

0

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)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.