asp.net-mvc 使用实体框架检测控制器中模型属性的状态变化

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16687825/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 02:51:57  来源:igfitidea点击:

Detect state change of model property, in controller, using Entity Framework

asp.net-mvcasp.net-mvc-3entity-framework

提问by Chopo87

I have a more or less standard looking model:

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

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

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

If the user modifies StartDateor project Duration, I have to call a function to update a simulation. In order to achieve this I'd like to detect the state change of the fields StartDateand Durationwithin a controller.

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

Something like that:

类似的东西:

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

Here is an example of what the Controller Method would look like:

以下是控制器方法的示例:

[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);
}

Any idea, how can I achieve this?

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

回答by Stefan Szasz

I believe you can compare the edited entity with the original one read from the database.

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

Something like:

就像是:

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);
}

回答by OrcaYlc

You can solve it by carrying old values via ViewBag.

您可以通过 ViewBag 携带旧值来解决它。

In action:

在行动:

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

Add hidden elements to View

向视图添加隐藏元素

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

Add these parameters to the post method and check them for changes

将这些参数添加到 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);
}

回答by KMan

If you want to do it without querying your persistance layer I guess adding the old values as fields in your model and then keep them in the page as hidden fields is the easiest way to solve this.

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

So add CurrentStartDate and CurrentDuration in your model:

因此,在您的模型中添加 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; }
}

and then add the hidden fields with the old values in your view:

然后在您的视图中添加具有旧值的隐藏字段:

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

This will give you something to compare the selected values with in your controller action.

这会给你一些东西来比较你的控制器动作中的选定值。

回答by Gene Stempel

Use .AsNoTracking().FirstOrDefault to retrieve the original object for comparison:

使用 .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);
}