Avatar of finance_teacher
finance_teacher

asked on 

ASP.net MVC4 -- History Log (UPDATE / DELETE)

I like
http://www.codeproject.com/Articles/34491/Implementing-Audit-Trail-using-Entity-Framework-Pa
but it is not for MVC4.

What easy "ASP.net MVC4 History Log"
solution do you have so the below works ?

Steps
 1. user clicks SAVE on Edit.cshtml as normal
 2. below code runs as normal
 3. somehow (via code or MSSQL database trigger) it writes
    only changed values to my "AUDIT_LOG" database table
-----------------------------------------------------------------
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Edit(MAINT_WORK_REQ maint_work_req)
        {
            if (ModelState.IsValid)
            {
                db.Entry(maint_work_req).State = EntityState.Modified;
                try
                {
                    //OurTechSignature_Date
                    if (maint_work_req.OurTechSignature_Date == null)
                    {
                        if (Request["Signature_Tech_Checked"].Contains("true"))
                        {
                            var context = new UsersContext();
                            UserProfile myProfile = context.UserProfiles.SingleOrDefault(u => u.UserName == User.Identity.Name);
                            maint_work_req.OurTechSignature_Date = myProfile.FirstName + " " + myProfile.LastName + " " + DateTime.Now.ToShortDateString();
                        }
                    }

                    var context2 = new UsersContext();
                    UserProfile myProfile2 = context2.UserProfiles.SingleOrDefault(u => u.UserName == User.Identity.Name);
                    maint_work_req.LastModifiedDate = DateTime.Now;
                    maint_work_req.LastModifiedBy = myProfile2.FirstName + " " + myProfile2.LastName;

                    db.SaveChanges();
                    .... something like "db.SaveAuditLog()" here
ASP.NETC#.NET Programming

Avatar of undefined
Last Comment
Aaron Jabamani
Avatar of Avodah
Avodah
Flag of United Kingdom of Great Britain and Northern Ireland image

I am not clear what is the question here?

There is not special solution for Audit logs in MVC. Solutions that work anywhere else will work with MVC and Entity Framework.

1. If you use a trigger then that gets executed at the database level and no need to worry about it in your c# code.

2. If you are manually saving to an audit table then simple insert into that table. Wrapping this in a transaction would be handy.

DaTribe
Avatar of finance_teacher
finance_teacher

ASKER

Do you have some sample code for your #2 solution ?
ASKER CERTIFIED SOLUTION
Avatar of Aaron Jabamani
Aaron Jabamani
Flag of United Kingdom of Great Britain and Northern Ireland image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
.NET Programming
.NET Programming

The .NET Framework is not specific to any one programming language; rather, it includes a library of functions that allows developers to rapidly build applications. Several supported languages include C#, VB.NET, C++ or ASP.NET.

137K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo