我已经广泛地比较了我的代码,但无法弄清楚出了什么问题。
状况
我有一张带有两个外键的学生(Alumnos)桌子,GROUP(Grupo)和TEACHER(Empleado)
我有一张带有三个外键的TEACHER(Empleado)表,SCHOOL(Escuela),GROUP(Grupo)和ROLE(Puesto)
问题
尝试使用除GROUP和TEACHER(从下拉列表)以外的所有内容创建STUDENTS记录时,我收到以下错误: 具有键'EmpleadoID'的ViewData项的类型为'System.Int32',但必须是'IEnumerable'类型。
然而,当我使用TEACHER记录尝试同样的事情时,一切正常,我在下拉列表旁边得到相应的验证消息,应该是这样。
这两种情况的代码显然不完全相同,但结构基本相同。
以下两组代码:
FROM STUDENTS
CONTROLLER
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);
}
MODEL
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")
}
来自教师</ p>
CONTROLLER
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);
}
MODEL
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")
}
据我所知,两者都应该可以正常工作,有什么我想念的吗?