Asp.net mvc下拉列表

时间:2013-01-30 00:07:20

标签: asp.net-mvc entity-framework

我有一个时间试图弄清楚我哪里出错了。情景是这样的。尝试使用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")
}

这就是观点。

关于初始get的视图,一切正常,但是当我发布EmployeeTypeId时,它的值为null。我检查源,选择列表很好,有适当的值和文本。请帮忙,因为我不知道我哪里出错了。

由于

2 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分: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)