FK constraint with ViewModel MVC4

本文关键字:MVC4 ViewModel with constraint FK | 更新日期: 2023-09-27 18:02:21

我有一个小问题使用MVC4和实体框架。我有一个实体"Person",它由另一个名为"ProductPackageCategories"的实体组成。我也有一个ViewModel"PersonViewModel"。在"Create Person"视图中,我可以创建我的新人员,并使用下拉列表组件指定类别。通常,当我单击提交按钮时,数据存储在数据库中。在我的例子中,我检索所有数据,但不检索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'.

语句已被终止。

My Create Actions in 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);
        }

My ViewModel:

public class PersonViewModel
{
    public Person person { get; set; }
    public PersonViewModel()
    {
        person = new Person();
    }
    public PersonViewModel(Person person)
    {
        this.person = person;
    }
}

My Create View:

@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")
}

FK constraint with ViewModel MVC4

Replace:

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

:

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

,以便在表单提交给HttpPost操作时,模型上的person.Id_ProductPackageCategory属性从下拉菜单中选择的值正确绑定。

使用strong -type -html-helpers将属性绑定到模型。用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" })