目前我的工作表中的行,其中包括以下變量中改變值:更新外鍵,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天。