English 中文(简体)
获取MVC3模型的旧值吗?
原标题:Capturing old values for model in MVC3?

我想在一个模型中捕捉旧值, 这样我就可以在提交后与新值进行比较, 并创建用户更改的审计日志 。

我猜是用隐藏的输入框来做,其中含有重复的旧价值属性,这是一种方法。 但想知道是否有其他好的替代方法吗?

谢谢 谢谢

最佳回答

在保存方法中,只需从数据库 < 坚固> 中获取原始对象,然后保存这些更改,然后您有新旧的数值可以比较? :

问题回答

这听起来像标准审计。你不应该担心什么改变了,什么改变了只是抓住了一切,谁做了改变。除非有某种实时报告需要做。

<强 > 可能的审计执行:

< a href=> "http://elegantcode.com/2009/11/11/cqrs-la-greg-young/" rel="nofollow" >CQRS , 简而言之,它跟踪特定对象的每一个变化。 其下端是它是一个更需要执行的架构。

滚动分类账。在数据库中,每插入一个新行。当前行用于显示目的,但每次更新后,将插入新行。

另一种办法是将其保存在审计表格中。

所有人完成我们的任务

您也可以将原始模型存储在视图袋中, 并做类似的事情...

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

我还没有机会测试这个,

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




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Transaction handling with TransactionScope

I am implementing Transaction using TransactionScope with the help this MSDN article http://msdn.microsoft.com/en-us/library/system.transactions.transactionscope.aspx I just want to confirm that is ...

System.Web.Mvc.Controller Initialize

i have the following base controller... public class BaseController : Controller { protected override void Initialize(System.Web.Routing.RequestContext requestContext) { if (...

Microsoft.Contracts namespace

For what it is necessary Microsoft.Contracts namespace in asp.net? I mean, in what cases I could write using Microsoft.Contracts;?

Separator line in ASP.NET

I d like to add a simple separator line in an aspx web form. Does anyone know how? It sounds easy enough, but still I can t manage to find how to do it.. 10x!

热门标签