【发布时间】: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