Validation Message - ASP.NET MVC

Asked

Viewed 440 times

1

Hello, I’m trying to get the message "validation" in ASP.NET in my project 'Cademeumedico' by the Web Development booklet with ASP.NET...

However, it is not appearing as it should appear below. inserir a descrição da imagem aqui

Mine appears like this:

inserir a descrição da imagem aqui

Follows the codes:

Add.cshtml (Views Folder)

@model CadeMeuMedico.Models.Medicos

@{
    ViewBag.Title = "Adicionar";
}

<h2>Adicionar</h2>

@using (Html.BeginForm()) { 
    @Html.AntiForgeryToken()
    @Html.ValidationSummary(true)

    <fieldset>
        <legend>Medico</legend>

        <div class="editor-label">
            @Html.LabelFor(model => model.CRM)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.CRM)
            @Html.ValidationMessageFor(model => model.CRM)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.Nome)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.Nome)
            @Html.ValidationMessageFor(model => model.Nome)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.Endereco)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.Endereco)
            @Html.ValidationMessageFor(model => model.Endereco)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.Bairro)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.Bairro)
            @Html.ValidationMessageFor(model => model.Bairro)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.Email)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.Email)
            @Html.ValidationMessageFor(model => model.Email)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.AtendePorConvenio)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.AtendePorConvenio)
            @Html.ValidationMessageFor(model => model.AtendePorConvenio)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.TemClinica)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.TemClinica)
            @Html.ValidationMessageFor(model => model.TemClinica)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.WebSiteBlog)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.WebSiteBlog)
            @Html.ValidationMessageFor(model => model.WebSiteBlog)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.Cidades)
        </div>
        <div class="editor-label">
            @Html.DropDownList("IDCidade", String.Empty)
            @Html.ValidationMessageFor(model => model.IDCidade)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.Especialidades)
        </div>
        <div class="editor-field">
            @Html.DropDownList("IDEspecialidade", String.Empty)
            @Html.ValidationMessageFor(model => model.IDEspecialidade)
        </div>

        <p>
            <input type="submit" value="Create" />
        </p>

    </fieldset>
}

<div>
    @Html.ActionLink("Back to list", "Index")
</div>

Medicoscontroller (Folder Controller)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using CadeMeuMedico.Models;
using System.Data.Entity;

namespace CadeMeuMedico.Controllers
{
    public class MedicosController : Controller
    {
        private CadeMeuMedicoBDEntities db = new CadeMeuMedicoBDEntities();
        //
        // GET: /Medicos/

        public ActionResult Index()
        {
            var medicos = db.Medicos.Include(m => m.Cidades).Include(m => m.Especialidades).ToList();
            return View(medicos);
        }

        public ActionResult Adicionar()
        {
            ViewBag.IDCidade = new SelectList(db.Cidades, "IDCidade", "Nome");
            ViewBag.IDEspecialidade = new SelectList(db.Especialidades, "IDEspecialidade", "Nome");
            return View();
        }

        [HttpPost]
        public ActionResult Adicionar(Medicos medicos)
        {
            if (ModelState.IsValid)
            {
                db.Medicos.Add(medicos);
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            ViewBag.IDCidade = new SelectList(db.Cidades, "IDCidade", "Nome", medicos.IDCidade);
            ViewBag.IDEspecialidade = new SelectList(db.Especialidades, "IDEspecialidade", "Nome", medicos.IDEspecialidade);
            return View(medicos);

        }

    }

Medicometdado.Cs (Models Folder)

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Web;

namespace CadeMeuMedico.Models
{
    [MetadataType(typeof(MedicoMetadado))]
    public partial class Medico
    {

    }

    public class MedicoMetadado
    {
        [Required(ErrorMessage = "Obrigatório informar o CRM")]
        [StringLength(30, ErrorMessage = "O CRM deve possuir no máximo 30 caracteres")]
        public string CRM { get; set; }

        [Required(ErrorMessage = "Obrigatório informar o Nome")]
        [StringLength(80, ErrorMessage = "O Nome deve possuir no máximo 80 caracteres")]
        public string Nome { get; set; }

        [Required(ErrorMessage = "Obrigatório informar o Endereço")]
        [StringLength(100, ErrorMessage = "O Endereço deve possuir no máximo 100 caracteres")]
        public string Endereco { get; set; }

        [Required(ErrorMessage = "Obrigatório informar o Bairro")]
        [StringLength(60, ErrorMessage = "O Bairro deve possuir no máximo 60 caracteres")]
        public string Bairro { get; set; }

        [Required(ErrorMessage = "Obrigatório informar o E-mail")]
        [StringLength(100, ErrorMessage = "O E-mail deve possuir no máximo 100 caracteres")]
        public string Email { get; set; }

        [Required(ErrorMessage = "Obrigatório informar se Atende por Convênio")]
        public bool AtendePorConvenio { get; set; }

        [Required(ErrorMessage = "Obrigatório informar se Tem Clínica")]
        public bool TemClinica { get; set; }

        [StringLength(80, ErrorMessage = "O Website deve possuir no máximo 80 caracteres")]
        public string WebsiteBlog { get; set; }

        [Required(ErrorMessage = "Obrigatório informar a Cidade")]
        public int IDCidade { get; set; }

        [Required(ErrorMessage = "Obrigatório informar a Especialidade")]
        public int IDEspecialidade { get; set; }
    }
}

If you prefer to see my github, follow the address below: https://github.com/joaowick/CadeMeuMedico

I count on your help!

1 answer

0


The problem is quite simple, you have a class of entity:

public partial class Medicos

and created a class partial:

public partial class Medico

That is, it is not the same entity class, so put a s at the end of the class that is missing that will solve the problem for you to reference the same class Medicos.

  • Hello, I changed and the error message appeared: The type of metadata associated with type 'Cademeumedico.Models.Medicos' contains the following properties or unknown fields: Websiteblog. Check that the names of these members match the names of the properties in the main type.

  • So @Joaotorresmoreira check if the models are the same...

  • The name is different in the two classes @Joaotorresmoreira one is Websiteblog and the other Websiteblog all this he checks

  • 1

    Wow, what a detail!!! It worked! Thank you Virgilio!!

Browser other questions tagged

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