我正在使用ASP.Net MVC 4框架开发在线商店。
我有一个对象类别 - 商店中的商品类别(例如男士服饰):
public class Category
{
[Required]
public long id { get; set; }
[Required]
public string name { get; set; }
public bool active { get; set; }
public bool displayOnTop { get; set; }
public bool showSubcategoriesItems { get; set; }
public Category parentCategory { get; set; }
public virtual Collection<Item> items { get; set; }
public Category()
{
this.items = new Collection<Item>();
}
}
有一个属性parentCategory - 这意味着此类别包含在另一个类别中(例如男士服装&gt;衬衫)。
要编辑现有的类别I,请使用以下操作:
[HttpPost]
public ActionResult EditCategory(Category editedCategory, string parentCategorySelector)
{
UsersContext db = new UsersContext();
Category existingCategory = db.categories.SingleOrDefault(c => c.id == editedCategory.id);
long parentCategoryId = (long)Int32.Parse(parentCategorySelector);
Category parentCategory = db.categories.SingleOrDefault(c => c.id == parentCategoryId);
if (existingCategory == null) return RedirectToAction("Index", "Admin");
existingCategory.name = editedCategory.name;
existingCategory.active = editedCategory.active;
existingCategory.displayOnTop = editedCategory.displayOnTop;
existingCategory.showSubcategoriesItems = editedCategory.showSubcategoriesItems;
existingCategory.parentCategory = parentCategory;
db.Entry(existingCategory.parentCategory).State = EntityState.Modified;
db.SaveChanges();
return View(existingCategory);
}
一切正常,除非我想将parentCategory设置为null - 它在代码中设置为null,但是null值没有保存到数据库 - 之前的值仍然存在。
但如果我这样做:
[HttpPost]
public ActionResult EditCategory(Category editedCategory, string parentCategorySelector)
{
UsersContext db = new UsersContext();
Category existingCategory = db.categories.SingleOrDefault(c => c.id == editedCategory.id);
long parentCategoryId = (long)Int32.Parse(parentCategorySelector);
Category parentCategory = db.categories.SingleOrDefault(c => c.id == parentCategoryId);
if (existingCategory == null) return RedirectToAction("Index", "Admin");
existingCategory.name = editedCategory.name;
existingCategory.active = editedCategory.active;
existingCategory.displayOnTop = editedCategory.displayOnTop;
existingCategory.showSubcategoriesItems = editedCategory.showSubcategoriesItems;
existingCategory.parentCategory = null;
db.Entry(existingCategory.parentCategory).State = EntityState.Modified;
db.SaveChanges();
return View(existingCategory);
}
即。我在没有条件的情况下将其设置为null,正确保存。
我做错了什么?
答案 0 :(得分:1)
为什么“类别”下没有Key属性。 ID ?如果 id 是主键,则必须在那里。
您应该使用包含到load relate entities:
[HttpPost]
public ActionResult EditCategory(Category editedCategory, string parentCategorySelector)
{
using(UsersContext db = new UsersContext())
{
Category existingCategory = db.categories.Include(c => c.parentCategory).SingleOrDefault(c => c.id == editedCategory.id);
long parentCategoryId = (long)Int32.Parse(parentCategorySelector);
Category parentCategory = db.categories.SingleOrDefault(c => c.id == parentCategoryId);
if (existingCategory == null) return RedirectToAction("Index", "Admin");
existingCategory.name = editedCategory.name;
existingCategory.active = editedCategory.active;
existingCategory.displayOnTop = editedCategory.displayOnTop;
existingCategory.showSubcategoriesItems = editedCategory.showSubcategoriesItems;
existingCategory.parentCategory = parentCategory;
db.SaveChanges();
return View(existingCategory);
}
}
请注意,DbContext如何正确处理。