5

我想捕获模型中的旧值,以便在提交后与新值进行比较,并创建用户所做更改的审核日志。

我的猜测是使用具有重复旧值属性的隐藏输入框来做这件事是一种方法。但想知道是否还有其他好的选择?

谢谢

4

4 回答 4

9

在 save 方法中,只需在保存更改之前从数据库中获取原始对象,然后您就可以比较旧值和新值了吗?:)

于 2012-05-24T16:47:59.390 回答
2

这听起来像是标准审计。您不必担心发生了什么变化,只需捕获所有内容以及做出更改的人。除非需要进行某种实时报告。

可能的审计实现:

CQRS,简而言之,它跟踪给定对象的每次更改。缺点是它是一个实施起来更复杂的架构。

滚动分类帐。每个插入都是数据库中的一个新行。最新行用于显示目的,但每次更新时,都会在数据库中插入一个新行。

另一种方法是将其保存到审计表中。

都把工作做好。

于 2012-05-24T17:06:23.070 回答
1

正是 mattytommo 所说的是首选方法

实例化新视图模型以创建新实体

public ActionResult Edit(int id) {
    var entity = new Entity(id); // have a constructor in your entity that will populate itself and return the instance of what is in the db
    // map entity to ViewModel using whatever means you use
    var model = new YourViewModel();
    return View(model);
}

发回更改

[HttpPost]
public ActionResult Edit(YourViewModel model) {
    if (ModelState.IsValid) {
        var entity = new YourEntity(model.ID); // re-get from db
       // make your comparison here
       if(model.LastUserID != entity.LastUserID // do whatever
       ... etc...
    }
    return View(model);
}
于 2012-05-24T16:53:48.290 回答
1

您还可以将原始模型存储在视图包中并执行以下操作...

// In the controller
public ActionResult DoStuff()
{
    // get your model
    ViewBag.OriginalModel = YourModel;
    return View(YourModel);
}

// In the View
<input type="hidden" name="originalModel" value="@Html.Raw(Json.Encode(ViewBag.OriginalModel));" />

// In the controller's post...
[HttpPost]
public ActionResult DoStuff(YourModel yourModel, string originalModel)
{
    // yourModel will be the posted data.
    JavaScriptSerializer JSS = new JavaScriptSerializer();
    YourModel origModel = JSS.Deserialize<YourModel>(originalModel);
}

我没有机会测试这个,只是一个理论:)

于 2012-05-24T16:59:59.307 回答