使用ViewModel MVC4的FK约束

时间:2013-03-04 13:27:39

标签: c# asp.net-mvc viewmodel

使用MVC4和Entity Framework时遇到了一些问题。我有一个实体“Person”,它由另一个名为“ProductPackageCategories”的实体组成。我还有一个ViewModel“PersonViewModel”。在“创建人员”视图中,我可以创建我的新人并使用下拉列表组件指定类别。通常,当我单击提交按钮时,数据将存储在数据库中。在我的例子中,我检索所有数据,但不检索ProductPackageCategory Id。有人对这个问题有所了解吗?

异常消息:

The INSERT statement conflicted with the FOREIGN KEY constraint "FK_bm_Persons_bm_ProductPackageCategories". The conflict occurred in database "C:\USERS\MAARAB\DESKTOP\PROJECT\2013-03-01_4-9_MA-OS_BUSI MATERIAL PROJECT\BUSIMATERIAL\BUSIMATERIAL\APP_DATA\BUSIMATERIAL.MDF", table "dbo.bm_ProductPackageCategories", column 'Id_ProductPackageCategory'.

声明已经终止。

我在PersonController中的创建操作:

public ActionResult Create()
        {
            ViewBag.Id_ProductPackageCategory = new SelectList(db.ProductPackageCategories, "Id_ProductPackageCategory", "Name");
            return View();
        }

        //
        // POST: /Person/Create

        [HttpPost]
        public ActionResult Create(PersonViewModel personViewModel)
        {

            if (ModelState.IsValid)
            {

                db.Persons.AddObject(personViewModel.person);
                db.SaveChanges();
                return RedirectToAction("Index");
            }



            ViewBag.Id_ProductPackageCategory = new SelectList(db.ProductPackageCategories, "Id_ProductPackageCategory", "Name", personViewModel.person.Id_ProductPackageCategory);

            return View(personViewModel);
        }

我的ViewModel:

public class PersonViewModel
{
    public Person person { get; set; }

    public PersonViewModel()
    {
        person = new Person();
    }

    public PersonViewModel(Person person)
    {
        this.person = person;
    }

}

我的创建视图:

@model BuSIMaterial.Models.PersonViewModel

@{
    ViewBag.Title = "Create";
}

<h2>Create</h2>

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

    <fieldset>
        <legend>Person</legend>

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

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

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

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

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

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

        <div class="editor-label">
            @Html.LabelFor(model => model.person.Id_ProductPackageCategory, "Category")
        </div>
        <div class="editor-field">
            @Html.DropDownList("Id_ProductPackageCategory", "Choose one...")
            @Html.ValidationMessageFor(model => model.person.Id_ProductPackageCategory)
        </div>

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

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

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

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

2 个答案:

答案 0 :(得分:0)

替换:

@Html.DropDownList("Id_ProductPackageCategory", "Choose one...")

使用:

@Html.DropDownListFor(
    model => model.person.Id_ProductPackageCategory,
    (SelectList)ViewBag.Id_ProductPackageCategory,
    "Choose one..."
)

以便在将表单提交到HttpPost操作时,模型中的person.Id_ProductPackageCategory属性与下拉列表中的选定值绑定正确。

答案 1 :(得分:0)

使用strong -typed-html-helpers将属性绑定到model。使用DropDownListFor代替DropDownList

控制器

public ActionResult Create()
{
    ViewBag.Id_ProductPackageCategory = new SelectList(db.ProductPackageCategories, "Id_ProductPackageCategory", "Name");
    return View();
}

视图

@Html.DropDownListFor(model => model.person.Id_ProductPackageCategory, ViewBag.Id_ProductPackageCategory as SelectList, "--- Select Category ---", new { @class = "some_class" })