使用Entity Framework检测控制器中模型属性的状态更改

时间:2013-05-22 09:09:40

标签: asp.net-mvc asp.net-mvc-3 entity-framework

我有一个或多或少的标准模型:

public class Project {
  public int ID { get; set; }
  //... some more properties

  public DateTime StartDate { get; set; }
  public int Duration { get; set; }
}

如果用户修改了StartDate或项目Duration,我必须调用一个函数来更新模拟。为了实现这一点,我想检测控制器中字段StartDateDuration的状态更改。

类似的东西:

if(project.StartDate.stateChange() || project.Duration.stateChange())

以下是Controller Method的示例:

[HttpPost]
public ActionResult Edit(Project project)
{
    if (ModelState.IsValid)
    {
        if(project.StartDate.stateChange() || project.Duration.stateChange())
            doSomething();

        db.Entry(project).State = EntityState.Modified;
        db.SaveChanges();
        return RedirectToAction("Index");
    }
    return View(project);
}

任何想法,我怎样才能做到这一点?

4 个答案:

答案 0 :(得分:9)

我相信您可以将已编辑的实体与从数据库中读取的原始实体进行比较。

类似的东西:

public ActionResult Edit(Project project)
{
    if (ModelState.IsValid)
    {
        var original = db.Find(project.ID);
        bool changed = original.StartDate != project.StartDate || original.Duration != project.Duration;
        if (changed)
        {
            original.StartDate = project.StartDate;
            original.Duration = project.Duration;
            doSomething();
            db.Entry(original).CurrentValues.SetValues(project);
            db.SaveChanges();
        }
    }
    return View(project);
}

答案 1 :(得分:7)

您可以通过ViewBag携带旧值来解决此问题。

行动中:

public ActionResult Edit(int? id)
{
    //...Your Code
    ViewBag.OrigStartDate = project.StartDate;
    ViewBag.OrigDuration = project.Duration;
    return View(project);
}

向视图添加隐藏元素

...
@Html.Hidden("OrigStartDate", (DateTime)ViewBag.OrigStartDate)
@Html.Hidden("OrigDuration", (int)ViewBag.OrigDuration)
...

将这些参数添加到post方法并检查它们的更改

[HttpPost]
public ActionResult Edit(DateTime OrigStartDate, int OrigDuration)
{
    if (ModelState.IsValid)
    {
        if (OrigStartDate != project.StartDate || OrigDuration != project.Duration)
            doSomething();

        db.Entry(project).State = EntityState.Modified;
        db.SaveChanges();
        return RedirectToAction("Index");
    }
    ViewBag.FileTypeId = new SelectList(db.FileTypes, "Id", "TypeName", dbinfo.FileTypeId);
    return View(project);
}

答案 2 :(得分:2)

如果你想在不查询持久层的情况下这样做,我想在模型中添加旧值作为字段,然后将它们保存在页面中,因为隐藏字段是解决此问题的最简单方法。

所以在模型中添加CurrentStartDate和CurrentDuration:

public class Project {
  public int ID { get; set; }
  //... some more properties

  public DateTime StartDate { get; set; }
  public int Duration { get; set; }

  public DateTime CurrentStartDate { get; set; }
  public int CurrentDuration { get; set; }
}

然后在视图中添加包含旧值的隐藏字段:

@Html.HiddenFor(model => model.CurrentStartDate )
@Html.HiddenFor(model => model.CurrentDuration )

这将为您提供在控制器操作中比较所选值的功能。

答案 3 :(得分:0)

使用.AsNoTracking()。FirstOrDefault检索原始对象以进行比较:

public ActionResult Edit(Project project)
{
    if (ModelState.IsValid)
    {
        Project original = db.AsNoTracking().FirstOrDefault( p => p.ID == project.ID);
        if (original.StartDate != project.StartDate || original.Duration != project.Duration)
            doSomething();

        db.Entry(project).State = EntityState.Modified;
        db.SaveChanges();
   }
   return View(project);
}