2017-04-24 134 views
0

目前我的工作表中的行,其中包括以下變量中改變值:更新外鍵,Entity.Modified

public int ID { get; set; } 
public string Item_Name { get; set; } 
[DisplayFormat(DataFormatString = "{0:0.##}")] 
public decimal Price { get; set; } 
public int TimeSlot { get; set; } 
public bool Food_AddOns { get; set; } 
public bool Drink_AddOns { get; set; } 
public virtual Item_Description Item_Description { get; set; } 
public virtual Item_Status Item_Status { get; set; } 
public virtual Dinner Dinner { get; set; } 
public string Ingredients { get; set; } 

我的視圖通過所給出的值從用戶到該模型:

public class Edit_AddItemModel 
{ 
[Display(Name = "ID")] 
public int ID { get; set; } 
[Display(Name = "Item Name:")] 
[StringLength(100, ErrorMessage = "The {0} must be at least {2} characters long.", MinimumLength = 1)] 
public string New_ItemName { get; set; } 
[DisplayFormat(DataFormatString = "{0:0.##}", ApplyFormatInEditMode = true)] 
[Display(Name = "Price:")] 
public decimal New_Price { get; set; } 
[Display(Name = "Time Slot:")] 
public int New_TimeSlot { get; set; } 
[Display(Name = "Lunch Special?:")] 
public bool New_Food_AddOns { get; set; } 
[Display(Name = "Free Drink?:")] 
public bool Drink_AddOns { get; set; } 
[Display(Name = "Item Description:")] 
public string New_Item_Description { get; set; } 
public bool New_spicy { get; set; } 
public bool New_gluten { get; set; } 
public bool New_vegetarian { get; set; } 
[Display(Name = "Dinner:")] 
public string New_Dinner { get; set; } 
[Display(Name = "Ingredients:")] 
[StringLength(140, ErrorMessage = "The {0} must be at least {2} characters long.", MinimumLength = 0)] 
public string New_Ingredients { get; set; } 
} 

我的值正確傳遞給控制器​​作爲圖像中示出的下方,並且被傳遞給函數調用Edit_CheckAllValues。這是Edit_CheckAllValues的樣子,基本建立與什麼已經從視圖傳遞了一個ItemsModel對象:

private ItemsModel Edit_CheckAllValues(ItemsModel NewItem, Edit_AddItemModel model) 
    { 
     int CurrentItem_ItemStatus; 
     NewItem.ID = model.ID; 
     NewItem.Item_Name = model.New_ItemName; 
     NewItem.Price = model.New_Price; 
     NewItem.TimeSlot = model.New_TimeSlot; 
     NewItem.Food_AddOns = model.New_Food_AddOns; 
     NewItem.Drink_AddOns = model.Drink_AddOns; 
     NewItem.Item_Description = convertToForeignKey_ItemDescription(Convert.ToInt32(model.New_Item_Description)); 
     //Get Current Item Status. 
     CurrentItem_ItemStatus = get_ItemStatus(model.New_spicy, model.New_gluten, model.New_vegetarian); 
     NewItem.Item_Status = convertToForeignKey_ItemStatus(CurrentItem_ItemStatus); 
     NewItem.Dinner = convertToForeignKey_Dinner(Convert.ToInt32(model.New_Dinner)); 
     NewItem.Ingredients = model.New_Ingredients; 
     return NewItem; 
    } 

的是返回什麼例子,有什麼新的ItemModel的樣子,它包含的ID我想編輯的項目:返回值:

的外鍵的一個實例,從ID被更改:20 ID:2項目外鍵更改:

的新對象然後傳回給t他原來的ActionResult EditItem,它正在改變項目的選擇的狀態,改變不屬於外鍵,如價格,商品名稱,時隙等變量時,其正常工作:

public ActionResult EditItem(EditItemModel model) 
    { 
     if (ModelState.IsValid) 
     { 
      ItemsModel newItem = new ItemsModel(); 
      newItem = Edit_CheckAllValues(newItem, model.edit_AddItemModel); 
      ApplicationDbContext db = new ApplicationDbContext(); 
      db.Items.Attach(newItem); //Tired both with Attach and Without Attach 
      db.Entry(newItem).State = EntityState.Modified; 
      db.SaveChanges(); 
     } 
     return RedirectToAction("ChangeItems", "Employee"); 
    } 

我不知道在我的代碼中,我需要進行編輯,但我的猜測是我需要在Edit_CheckAllValues中首先獲取行,而不是將值傳遞給新對象並使用EntityState.Modified將其發送到數據庫。

任何幫助將不勝感激,因爲我一直在這個問題上停留了3天。

回答

0

基於關閉引用此頁面(ASP.Net MVC 4 Update value linked to a foreign key column

我能找出問題。我需要更新的部分是我ItemsModel:

public class ItemsModel 
{ 
    public int ID { get; set; } 
    public string Item_Name { get; set; } 
    [DisplayFormat(DataFormatString = "{0:0.##}")] 
    public decimal Price { get; set; } 
    public int TimeSlot { get; set; } 
    public bool Food_AddOns { get; set; } 
    public bool Drink_AddOns { get; set; } 
    [ForeignKey("Item_Description")] 
    public virtual int Item_Description_ID { get; set; } 
    public virtual Item_Description Item_Description { get; set; } 
    [ForeignKey("Item_Status")] 
    public virtual int Item_Status_ID { get; set; } 
    public virtual Item_Status Item_Status { get; set; } 
    [ForeignKey("Dinner")] 
    public virtual int Dinner_ID { get; set; } 
    public virtual Dinner Dinner { get; set; } 
    public string Ingredients { get; set; } 
} 

添加[ForeignKey的]從API:使用System.ComponentModel.DataAnnotations.Schema;

和更新我的Edit_CheckAllValues功能:

private ItemsModel Edit_CheckAllValues(ItemsModel NewItem, Edit_AddItemModel model) 
    { 
     int CurrentItem_ItemStatus; 
     NewItem.ID = model.ID; 
     NewItem.Item_Name = model.New_ItemName; 
     NewItem.Price = model.New_Price; 
     NewItem.TimeSlot = model.New_TimeSlot; 
     NewItem.Food_AddOns = model.New_Food_AddOns; 
     NewItem.Drink_AddOns = model.Drink_AddOns; 
     //NewItem.Item_Description = convertToForeignKey_ItemDescription(Convert.ToInt32(model.New_Item_Description)); 
     NewItem.Item_Description_ID = Convert.ToInt32(model.New_Item_Description); 
     //Get Current Item Status. 

     CurrentItem_ItemStatus = get_ItemStatus(model.New_spicy, model.New_gluten, model.New_vegetarian); 
     NewItem.Item_Status_ID = CurrentItem_ItemStatus; 
     // NewItem.Item_Status = convertToForeignKey_ItemStatus(CurrentItem_ItemStatus); 
     NewItem.Dinner_ID = 163; //Ignore this is my empty default Dinner. 
     // NewItem.Dinner = convertToForeignKey_Dinner(Convert.ToInt32(model.New_Dinner)); 
     // CurrentItem.Item_Description = convertToForeignKey_ItemDescription(Convert.ToInt32(model.New_Item_Description)); 
     NewItem.Ingredients = model.New_Ingredients; 
     return NewItem; 
    } 

實體改性最終將辦理變更登記的外鍵。