I am using the Entity framework for data persistence, I want to save an id (the field is of type identity), a name and the gender of the person, but when I click on save, it throws an exception to me
Error de servidor en la aplicación '/'.
Validation failed for one or more entities. See 'EntityValidationErrors' property for more details.
Descripción: Excepción no controlada al ejecutar la solicitud Web actual. Revise el seguimiento de la pila para obtener más información acerca del error y dónde se originó en el código.
Detalles de la excepción: System.Data.Entity.Validation.DbEntityValidationException: Validation failed for one or more entities. See 'EntityValidationErrors' property for more details.
Error de código fuente:
Línea 25: {
Línea 26: db.Student.Add(st);
Línea 27: db.SaveChanges();
Línea 28:
Línea 29: return RedirectToAction("Index", "Home");
Archivo de origen: C:\Users\Usuario\Documents\Visual Studio 2015\Projects\practicando\pruebas\Pruebas\Lista\Controllers\HomeController.cs Línea: 27
and I can not continue. What's happening?
Controller:
using Lista.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace Lista.Controllers
{
public class HomeController : Controller
{
// GET: Home
public ActionResult Index()
{
return View();
}
private pruebaEntities db = new pruebaEntities();
[HttpPost]
public ActionResult Index(Student st)
{
if (ModelState.IsValid)
{
db.Student.Add(st);
db.SaveChanges();
return RedirectToAction("Index", "Home");
}
return View(st);
}
}
}
Model:
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Web;
namespace Lista.Models
{
public class Student
{
public int StudentId { get; set; }
[Display(Name = "Name")]
[Required]
public string StudentName { get; set; }
[Required]
public Gender StudentGender { get; set; }
}
public enum Gender
{
Male,
Female
}
}
Vista:
@using Lista.Models
@{
Layout = null;
}
@using (Html.BeginForm("CrearCliente"))
{
Nombre: <br />
<input id="Text1" type="text" />
@Html.DropDownList("StudentGender",
new SelectList(Enum.GetValues(typeof(Gender))),
"Select Gender",
new { @class = "form-control" })
<input type="submit" value="Guardar" />
}
I do not put in the view @model because or else the enum does not take me