Hola! Les muestro:
Mi Clase:
Código C++:
Ver originalusing System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations;
namespace Clase6.Models
{
public class Usuario
{
[Required]
[StringLength(25, MinimumLength = 3, ErrorMessage = "El nombre debe tener entre 3 y 25 letras")]
public string Nombre { set; get; }
[Required]
[StringLength(25, MinimumLength = 3, ErrorMessage = "El apellido debe tener entre 3 y 25 letras")]
public string Apellido { set; get; }
public Perfil Perfil { get; set; }
[Required]
[Range(1,25, ErrorMessage = "El campo perfil es requerido")]
public int IdPerfil { get; set; }
}
}
Mi Formulario:
Código C++:
Ver original@{
ViewBag.Title = "Crear";
Layout = "~/Views/_shared/_Layout.cshtml";
}
@model Clase6.Models.Usuario
<form role="form" method="post">
@Html.LabelFor(model => model.Nombre, new { @for = "txtNombre" })
@Html.TextBoxFor(model => model.Nombre, new { @class = "form-control" })
@Html.ValidationMessageFor(model => model.Nombre , "", new { @class = "text-danger" })
@Html.LabelFor(model => model.Apellido, new { @for = "txtApellido" })
@Html.TextBoxFor(model => model.Apellido, new { @class = "form-control" })
@Html.ValidationMessageFor(model => model.Apellido)
@Html.Label("Seleccionar Perfil", new { @for = "txtIdPerfil" })
@Html.DropDownListFor(model => model.IdPerfil, new SelectList(ViewBag.perfiles, "Id", "Descripcion"), new { @class = "form-control" })
@Html.ValidationMessageFor(model => model.IdPerfil)
<br />
<button type="submit" class="btn btn-primary btn-block">Enviar</button>
</form>
Alguno logra darse cuenta por que no me esta funcionando la validacion? Me estoy olvidando de algo? Gracias!