调试时将值设置为null,而不是在运行时设置为null

时间:2012-09-30 22:19:20

标签: c# asp.net linq entity-framework linq-to-entities

我的类别可以包含子类别/根类别。当我将我的根类别更改为其他类别时,它可以正常工作,但是当我尝试将其设置为null(使其成为根类别)时,它不会改变任何内容。我正在使用代码优先方法,如果这有任何区别。

下面是代码,我明确声明category.RootCategory = null但它不起作用,根类别仍然是先前设置的。

控制器

[HttpPost]
public ActionResult Edit(Category c, int? rootCategoryID)
{
    var category = _db.Categories.Where(x => x.ID == c.ID).Single();

    if (TryUpdateModel(category))
    {
        cateogry.RootCategory = null;
            _db.SaveChanges();
        return Content(Infrastructure.Helpers.SerializeObject(category));
    }

    return Redirect("/admin/category");
}

模型

public class Category
{
    public int ID { get; set; }
    virtual public Category RootCategory { get; set; }
    virtual public ICollection<Category> ChildCategories { get; set; }
}

更新

非常奇怪的行为。当我调试它并逐步地逐步进行更新时,当我不调试它或快速运行时它不会更新。我不知道为什么会这样。

1 个答案:

答案 0 :(得分:1)

无论如何,我将public int? RootCategoryID { get; set; }添加到我的模型中,现在我可以像这样编辑它。

[HttpPost]
public ActionResult Edit(Category c)
{
    var cateogry = _db.Categories.Where(x => x.ID == c.ID).Single();

    if (TryUpdateModel(cateogry))
    {
        cateogry.UpdatedDateTime = DateTime.Now;
        _db.SaveChanges();
    }

    return Redirect("/admin/category");
}

它现在有效,但仍然不知道为什么它在运行时没有RootCategoryID就可以工作,并且在调试时也可以。

相关问题