0

我有一段时间试图找出我哪里出错了。情景是这样的。尝试使用 mvc 实体框架添加一个具有外键的对象。

public class Person
{
    [Key]
    public virtual int PersonId { get; set; }
    [Display(Name = "First Name")]
    [Required]
    [MaxLength(50)]
    public virtual string FirstName { get; set; }
    [Display(Name = "Last Name")]
    [Required]
    [MaxLength(50)]
    public virtual string LastName { get; set; }
    [Required]
    public virtual int TestItem { get; set; }
    [Required(ErrorMessage = "Please Select an Employee Type")]
    public virtual EmployeeType EmployeeTypeId { get; set; }
}


 public class EmployeeType
{
    [Key]
    public virtual int EmployeeTypeId { get; set; }
    [Required]
    public virtual string EmployeeTypeName { get; set; }


}

这些是实体的两个 pocos。

 public ActionResult Create()
    {
        PersonViewModel pv = new PersonViewModel();
        ViewBag.EmployeeTypeSelect= new SelectList(db.EmployeeTypes, "EmployeeTypeId", "EmployeeTypeName");
        return View(pv);
    }

    //
    // POST: /Person/Create

    [HttpPost]
    public ActionResult Create(PersonViewModel personVM)
    {
        if (ModelState.IsValid)
        {
            Person person = new Person();
            person.EmployeeTypeId = personVM.EmployeeTypeId;
            person.FirstName = personVM.FirstName;
            person.LastName = personVM.LastName;
            person.TestItem = personVM.TestItem;
            db.Persons.Add(person);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        return View(personVM);
    }

那是控制器。

@model MvcApplication3.Models.PersonViewModel

@{
ViewBag.Title = "Create";
}

<h2>Create</h2>

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

<fieldset>
    <legend>PersonViewModel</legend>

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

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

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

    <div class="editor-label">
        @Html.LabelFor(model => model.EmployeeTypeId)
    </div>
    <div class="editor-field">
        @Html.DropDownListFor(model=>model.EmployeeTypeId,(SelectList)ViewBag.EmployeeTypeSelect)
        @Html.ValidationMessageFor(model => model.EmployeeTypeId)
    </div>




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

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

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

这就是观点。

初始获取的视图一切正常,但是当我发布 EmployeeTypeId 有一个空值。我检查了来源,选择列表很好,有正确的值和文本。请帮忙,因为我不知道我哪里出错了。

谢谢

4

2 回答 2

0

您的 Person 和 EmployeeType 类需要有一个默认(空)构造函数才能使模型绑定起作用 -

 public class EmployeeType
{
    [Key]
    public virtual int EmployeeTypeId { get; set; }
    [Required]
    public virtual string EmployeeTypeName { get; set; }

    public EmployeeType()
    {
    }
}

public class Person {
    [Key]
    public virtual int PersonId { get; set; }
    [Display(Name = "First Name")]
    [Required]
    [MaxLength(50)]
    public virtual string FirstName { get; set; }
    [Display(Name = "Last Name")]
    [Required]
    [MaxLength(50)]
    public virtual string LastName { get; set; }
    [Required]
    public virtual int TestItem { get; set; }
    [Required(ErrorMessage = "Please Select an Employee Type")]
    public virtual EmployeeType EmployeeTypeId { get; set; }

    public Person()
    {
       EmployeeTypeId = new EmployeeType();
    }
}

另外,尝试在您的视图中,使其

@Html.DropDownListFor(model=>model.EmployeeTypeId.EmployeeTypeId,(SelectList)ViewBag.EmployeeTypeSelect)

于 2013-01-30T00:35:56.567 回答
0

尝试在您希望发布的所有值之后在 HTML 中添加 Html.EndForm() 并查看是否可以解决问题?

于 2013-01-30T00:23:59.373 回答