1

MVC4 と Entity Framework の使用に少し問題があります。「ProductPackageCategories」という名前の他のエンティティで構成されるエンティティ「Person」があります。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'.

ステートメントは終了されました。

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

2 に答える 2

0

交換:

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

と:

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

person.Id_ProductPackageCategoryフォームが HttpPost アクションに送信されるときに、モデルのプロパティがドロップダウンで選択された値から正しくバインドされるようにします。

于 2013-03-04T13:32:44.887 に答える
0

強く型付けされた 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" })
于 2013-03-04T13:32:46.443 に答える