2017-04-24 72 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; 
    } 

实体改性最终将办理变更登记的外键。