2013-03-04 82 views
1

我有一個小問題使用MVC4和實體框架。我有一個由名爲「ProductPackageCategories」的其他實體組成的實體「Person」。我也有一個ViewModel「PersonViewModel」。在「創建人員」視圖中,我可以創建我的新人並使用下拉列表組件指定類別。通常,當我點擊提交按鈕時,數據存儲在數據庫中。在我的情況下,我檢索所有的數據,但不是ProductPackageCategory標識。有人對這個問題有所瞭解嗎?FK約束與ViewModel MVC4

異常消息:

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); 
     } 

我的視圖模型:

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

回答

0

替換:

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

有:

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

讓你的模型person.Id_ProductPackageCategory屬性是從選定的價值在下拉列表中正確綁定當表單被提交到您的HttpPost行動。

+0

感謝您的幫助!只是一個小問題,對於「model => model.person.Id_ProductPackageCategory」部分,VS顯示錯誤「無法將lambda表達式轉換爲類型'string',因爲它不是委託類型」... – Traffy 2013-03-04 13:37:53

+0

我的錯誤,我忘了修改「DropdownListFor」中的「DropDownList」,謝謝! – Traffy 2013-03-04 13:41:51

0

使用strong-typed-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" }) 
+0

感謝您的幫助!只是一個小問題,對於「model => model.person.Id_ProductPackageCategory」部分,VS顯示錯誤「無法將lambda表達式轉換爲類型'string',因爲它不是委託類型」... – Traffy 2013-03-04 13:37:12

+0

請確保您添加'使用System.Data','使用System.Data.Entity'和'使用System.Linq;' – 2013-03-04 13:42:39

+0

我的錯誤,我忘了修改「DropDownList」中的「DropdownListFor」,謝謝! – Traffy 2013-03-04 13:42:40