Reputation: 663
I'm developing online store, using ASP.Net MVC 4 framework.
I have an object Category — category of items in store (men's apparel — for instance):
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>();
}
}
There is a property parentCategory — which means that this category is included to another one (Men's apparel > Shirts, for instance).
For editing existing Category I use the following action:
[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);
}
Everything works fine, except when I want to set parentCategory to null — it sets to null in code, but null value is not being saved to database — previous value still there.
But if I do it like this:
[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);
}
i.e. I set it to null without conditions, it is being saved correctly.
What am I doing wrong?
Upvotes: 0
Views: 2081
Reputation: 7585
Why there is no Key attribute under Category.id? If the id is a primary key it must be there.
You should use Include to 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);
}
}
Please note, how DbContext is properly disposed.
Upvotes: 1