Asp.net mvc 对记录创建MVC进行验证时出错

Asp.net mvc 对记录创建MVC进行验证时出错,asp.net-mvc,validation,Asp.net Mvc,Validation,我已经对我的代码进行了广泛的比较,但无法找出错误所在 形势 我有一张带两个外键的学生(校友)表,组(Grupo)和教师(Empleado) 我有一个教师(Empleado)表,表中有三个外键,分别是SCHOOL(Escuela)、GROUP(Grupo)和ROLE(Puesto) 问题 尝试创建除组和教师(从下拉列表中)之外的所有学生记录时,出现以下错误: 具有键“Emplicatoid”的ViewData项的类型为“System.Int32”,但必须为“IEnumerable”类型 然而,当我

我已经对我的代码进行了广泛的比较,但无法找出错误所在

形势

我有一张带两个外键的学生(校友)表,组(Grupo)和教师(Empleado)

我有一个教师(Empleado)表,表中有三个外键,分别是SCHOOL(Escuela)、GROUP(Grupo)和ROLE(Puesto)

问题

尝试创建除组和教师(从下拉列表中)之外的所有学生记录时,出现以下错误: 具有键“Emplicatoid”的ViewData项的类型为“System.Int32”,但必须为“IEnumerable”类型

然而,当我尝试对教师记录进行同样的操作时,一切都正常,我会在下拉列表旁边得到相应的验证消息,这是应该的

这两种情况的代码显然不相同,但结构基本相同

以下是两组代码:

来自学生

控制器

   public ActionResult Create()
    {

        var AluGru = from g in db.Grupoes
                     where g.ID != 1
                     select g;

        var AluEmp = from e in db.Empleadoes
                     where e.PuestoID != 1
                     select e;

        ViewBag.GrupoID = new SelectList(AluGru, "ID", "Grupo1");
        ViewBag.EmpleadoID = new SelectList(AluEmp, "ID", "FullName");

        return View();
    }

    // POST: Alumnoes/Create
    // To protect from overposting attacks, please enable the specific properties you want to bind to, for 
    // more details see http://go.microsoft.com/fwlink/?LinkId=317598.
    [HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult Create([Bind(Include = "ID,Nombre,Apellido,Telefono,Estado,GrupoID,EmpleadoID")] Alumno alumno)
    {
        if (ModelState.IsValid)
        {
            db.Alumnoes.Add(alumno);
            db.SaveChanges();
            return RedirectToAction("../");
        }

        ViewBag.GrupoID = new SelectList(db.Grupoes, "ID", "Grupo1", alumno.GrupoID);
        ViewBag.EmlpeadoID = new SelectList(db.Empleadoes, "ID", "Nombre", alumno.EmpleadoID);

        return View(alumno);
    }
模型

看法

模型

公共部分类Empleado
{
[System.Diagnostics.CodeAnalysis.SuppressMessage(“Microsoft.Usage”,“CA2214:DoNotCallOverridableMethodsInConstructors”)]
公共雇员
{
this.Alumnoes=new HashSet();
}
公共int ID{get;set;}
[必需(AllowEmptyStrings=false,ErrorMessage=“Nombre es requeriedo”)]
公共字符串Nombre{get;set;}
[必需(AllowEmptyStrings=false,ErrorMessage=“Nombre es requeriedo”)]
公共字符串Apellido{get;set;}
公共字符串全名{get{return Nombre+“”+Apellido;}
公共整数集合{get;set;}
公共int PuestoID{get;set;}
公共int GrupoID{get;set;}
公共虚拟Escuela Escuela{get;set;}
公共虚拟Grupo Grupo{get;set;}
公共虚拟Puesto Puesto{get;set;}
[System.Diagnostics.CodeAnalysis.SuppressMessage(“Microsoft.Usage”,“CA2227:CollectionPropertiesShouldBreadOnly”)]
公共虚拟ICollection校友{get;set;}
}
看法

@model\u 3E\u III.Models.Empleado
@{
ViewBag.Title=“创建”;
}
创造
@使用(Html.BeginForm())
{
@Html.AntiForgeryToken()
雇佣

@Html.ValidationSummary(true,“,new{@class=“text danger”}) @LabelFor(model=>model.Nombre,htmlAttributes:new{@class=“controllabel col-md-2”}) @EditorFor(model=>model.Nombre,new{htmlAttributes=new{@class=“form control”}}) @Html.ValidationMessageFor(model=>model.Nombre,“,new{@class=“text danger”}) @LabelFor(model=>model.Apellido,htmlAttributes:new{@class=“controllabel col-md-2”}) @EditorFor(model=>model.Apellido,new{htmlAttributes=new{@class=“form control”}}) @Html.ValidationMessageFor(model=>model.Apellido,“,new{@class=“text danger”}) @LabelFor(model=>model.escueled,“Escuela”,htmlAttributes:new{@class=“controllabel col-md-2”}) @DropDownList(“escueled”,null,“Selecciona una escuela”,htmlAttributes:new{@class=“form control”}) @Html.ValidationMessageFor(model=>model.escueled,“,new{@class=“text danger”}) @LabelFor(model=>model.PuestoID,“Puesto”,htmlAttributes:new{@class=“controllabel col-md-2”}) @DropDownList(“PuestoID”,null,“Selecciona un puesto”,htmlAttributes:new{@class=“form control”}) @Html.ValidationMessageFor(model=>model.PuestoID,“,new{@class=“text danger”}) @LabelFor(model=>model.GrupoID,“Grupo”,htmlAttributes:new{@class=“controllabel col-md-2”}) @DropDownList(“GrupoID”,null,“Selecciona un grupo”,htmlAttributes:new{@class=“form control”}) @Html.ValidationMessageFor(model=>model.GrupoID,“,new{@class=“text danger”}) } @ActionLink(“返回列表”和“./”) @节脚本{ @Scripts.Render(“~/bundles/jqueryval”) }

据我所知,两者都可以正常工作,有什么我遗漏的吗?

不确定这对主要问题是否重要,但有一个bug。在您指定的创建方法的GET版本中:ViewBag.EmpleadoID=new SelectList(AluEmp,“ID”,“FullName”);但之后在POST版本中,您将根据Nombre属性而不是全名分配文本:ViewBag.EmlpeadoID=new SelectList(db.Empleadoes,“ID”,“Nombre”,alumno.EmpleadoID);我从FullName恢复到Nombre,得到了相同的错误,顺便说一句,错误指向这一行:第61行:@Html.DropDownList(“Emplicatoid”,null,“Seleciona un maestro”,htmlAttributes:new{@class=“form control”}),考虑到填充的所有字段,它可以用FullName保存到数据库中。
public partial class Alumno
{
    public int ID { get; set; }
    [Required(AllowEmptyStrings = false, ErrorMessage = "Nombre es requerido")]
    public string Nombre { get; set; }
    [Required(AllowEmptyStrings = false, ErrorMessage = "Apellido es requerido")]
    public string Apellido { get; set; }
    [Required(AllowEmptyStrings = false, ErrorMessage = "Telefono es requerido")]
    public string Telefono { get; set; }
    [Required]
    public string Estado { get; set; }
    [Required]
    public int GrupoID { get; set; }
    [Required]
    public int EmpleadoID { get; set; }


    public virtual Grupo Grupo { get; set; }
    public virtual Empleado Empleado { get; set; }
}
@model _3E_III.Models.Alumno

@{
    ViewBag.Title = "Create";
}

<h2>Create</h2>


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

    <div class="form-horizontal">
    <h4>Alumno</h4>
    <hr />
    @Html.ValidationSummary(true, "", new { @class = "text-danger" })
    <div class="form-group">
        @Html.LabelFor(model => model.Nombre, htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(model => model.Nombre, new { htmlAttributes = new { @class = "form-control" } })
            @Html.ValidationMessageFor(model => model.Nombre, "", new { @class = "text-danger" })
        </div>
    </div>

    <div class="form-group">
        @Html.LabelFor(model => model.Apellido, htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(model => model.Apellido, new { htmlAttributes = new { @class = "form-control" } })
            @Html.ValidationMessageFor(model => model.Apellido, "", new { @class = "text-danger" })
        </div>
    </div>

    <div class="form-group">
        @Html.LabelFor(model => model.Telefono, htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(model => model.Telefono, new { htmlAttributes = new { @class = "form-control" } })
            @Html.ValidationMessageFor(model => model.Telefono, "", new { @class = "text-danger" })
        </div>
    </div>

    <div id="hidestate" class="form-group">
        @Html.LabelFor(model => model.Estado, htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(model => model.Estado, new { htmlAttributes = new { @class = "form-control", @Value = "Inactivo" } })
            @Html.ValidationMessageFor(model => model.Estado, "", new { @class = "text-danger" })
        </div>
    </div>

    <div class="form-group">
        @Html.LabelFor(model => model.GrupoID, "Grupo", htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.DropDownList("GrupoID", null, "Selecciona un grupo", htmlAttributes: new { @class = "form-control" })
            @Html.ValidationMessageFor(model => model.GrupoID, "", new { @class = "text-danger" })
        </div>
    </div>

    <div class="form-group">
        @Html.LabelFor(model => model.EmpleadoID, "Maestro", htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.DropDownList("EmpleadoID", null, "Selecciona un maestro", htmlAttributes: new { @class = "form-control" })
            @Html.ValidationMessageFor(model => model.EmpleadoID, "", new { @class = "text-danger" })
        </div>
    </div>

        <div class="form-group">
            <div class="col-md-offset-2 col-md-10">
                <input type="submit" value="Create" class="btn btn-default" />
            </div>
        </div>
    </div>
}

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

@section Scripts {
    @Scripts.Render("~/bundles/jqueryval")
}
    public ActionResult Create()
    {

        var GID = (from g in db.Grupoes
                   where g.ID != 1
                   select g);

        var PID = (from p in db.Puestoes
                   where p.ID != 1
                   select p);

        ViewBag.EscuelaID = new SelectList(db.Escuelas, "ID", "Nombre");
        ViewBag.GrupoID = new SelectList(GID, "ID", "Grupo1");
        ViewBag.PuestoID = new SelectList(PID, "ID", "Puesto1");
        return View();
    }

    // POST: Empleadoes/Create
    // To protect from overposting attacks, please enable the specific properties you want to bind to, for 
    // more details see http://go.microsoft.com/fwlink/?LinkId=317598.
    [HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult Create([Bind(Include = "ID,Nombre,Apellido,EscuelaID,PuestoID,GrupoID")] Empleado empleado)
    {
        if (ModelState.IsValid)
        {
            db.Empleadoes.Add(empleado);
            db.SaveChanges();
            return RedirectToAction("../");
        }

        ViewBag.EscuelaID = new SelectList(db.Escuelas, "ID", "Nombre", empleado.EscuelaID);
        ViewBag.GrupoID = new SelectList(db.Grupoes, "ID", "Grupo1", empleado.GrupoID);
        ViewBag.PuestoID = new SelectList(db.Puestoes, "ID", "Puesto1", empleado.PuestoID);
        return View(empleado);
    }
public partial class Empleado
{
    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
    public Empleado()
    {
        this.Alumnoes = new HashSet<Alumno>();
    }

    public int ID { get; set; }
    [Required(AllowEmptyStrings = false, ErrorMessage = "Nombre es requerido")]
    public string Nombre { get; set; }
    [Required(AllowEmptyStrings = false, ErrorMessage = "Nombre es requerido")]
    public string Apellido { get; set; }

    public string FullName { get { return Nombre + " " + Apellido; } }

    public int EscuelaID { get; set; }
    public int PuestoID { get; set; }
    public int GrupoID { get; set; }

    public virtual Escuela Escuela { get; set; }
    public virtual Grupo Grupo { get; set; }
    public virtual Puesto Puesto { get; set; }
    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
    public virtual ICollection<Alumno> Alumnoes { get; set; }
}
@model _3E_III.Models.Empleado

@{
    ViewBag.Title = "Create";
}

<h2>Create</h2>


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

    <div class="form-horizontal">
        <h4>Empleado</h4>
        <hr />
        @Html.ValidationSummary(true, "", new { @class = "text-danger" })
        <div class="form-group">
            @Html.LabelFor(model => model.Nombre, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.Nombre, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.Nombre, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.Apellido, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.Apellido, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.Apellido, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.EscuelaID, "Escuela", htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.DropDownList("EscuelaID", null, "Selecciona una escuela", htmlAttributes: new { @class = "form-control" })
                @Html.ValidationMessageFor(model => model.EscuelaID, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.PuestoID, "Puesto", htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.DropDownList("PuestoID", null, "Selecciona un puesto", htmlAttributes: new { @class = "form-control" })
                @Html.ValidationMessageFor(model => model.PuestoID, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.GrupoID, "Grupo", htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.DropDownList("GrupoID", null, "Selecciona un grupo", htmlAttributes: new { @class = "form-control" })
                @Html.ValidationMessageFor(model => model.GrupoID, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            <div class="col-md-offset-2 col-md-10">
                <input type="submit" value="Create" class="btn btn-default" />
            </div>
        </div>
    </div>
}

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

@section Scripts {
    @Scripts.Render("~/bundles/jqueryval")
}