【问题标题】:Update Foreign Key within row ASP.NET MVC, Entity.Modified更新 ASP.NET MVC、Entity.Modified 行中的外键
【发布时间】:2017-04-24 17:29:41
【问题描述】:

目前我正在更改表格行中的值,其中包括以下变量:

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

My View 将用户提供的值传递给此模型:

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 的示例 项目 描述 外键更改:

然后将新对象传递回原来的 ActionResult EditItem,它正在更改所选项目的状态,当更改价格、项目名称、TimeSlot 等非外键变量时,它可以正常工作:

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 将其发送到数据库。已修改。

任何帮助将不胜感激,因为我已经在这个问题上停留了 3 天。

【问题讨论】:

    标签: c# asp.net asp.net-mvc model-view-controller foreign-keys


    【解决方案1】:

    基于对本页的引用 (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; }
    }
    

    从 API 添加 [ForeignKey]:使用 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;
        }
    

    实体修改后将最终注册外键的更改。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-08-20
      • 2016-01-24
      • 2018-09-18
      • 2021-12-31
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多