I’ve created an Audit table for an ASP.NET MVC application to track key changes and actions. I want to present the contents of this table to authorized users in an easily readable format on the view.
The Audit table is (simplified) like so:
ID (int) | StaffID (int) | Action (string) | Timestamp (datetime)
-----------------------------------------------------------------
1987 | 27 | Delete entry: 9 | 2010-02-22 12:30:12
1988 | 34 | Add user: 912 | 2010-02-22 12:48:19
So far I’ve just been presenting that using the default “List” view in MVC however we’re getting towards the end of development and I’d like to tidy up this view a bit by showing staff names rather than StaffIDs.
Initially, I’m using the approach of creating a “hybrid model” that contains both Audit and Staff and passing that to the view:
public class AuditModel
{
public AuditModel(List<Audit> AuditEntries, List<Staff> AllStaff)
{
this.Audit = AuditEntries;
this.Staff = AllStaff;
}
public List<Audit> Audit { get; private set; }
public List<Staff> Staff { get; private set; }
}
[AcceptVerbs("GET")]
public ActionResult ViewAuditTrail()
{
var Audit = (from a in db.Audits orderby a.Timestamp descending select a).ToList();
var Staff = (from s in db.Staffs select s).ToList();
return View(new AuditModel(Audit, Staff));
}
But that leads to messiness in the view:
<%
foreach (var Entry in Model.AuditEntries)
{
var StaffDetails = Model.AllStaff.Where(s => s.ID == Entry.StaffID).SingleOrDefault();
/* output HTML */
}
%>
So I guess what I want to do is create a new model with the following attributes:
- ID (int) – Audit.ID
- StaffName (string) – Staff.ID [s => s.StaffID == Staff.ID]
- Action (string) – Audit.Action
- Timestamp (datetime) – Audit.Timestamp
But I want to do this in the controller and pass it to the view as a ToList() so my view can be cleaner and simpler.
Any tips?
Okay, so I got adventurous and tried to figure this out myself and I’ve got a solution but I’m not sure how correct it is!
So, firstly, I created a new class:
I then refactored my ViewAuditTrail action to look like so:
And now my ViewAuditTrail view is just a strongly-typed view bound to the AuditEntries class using the default MVC list view type.
It seems to work fine but I’m sure it can be improved upon – does anyone have any suggestions?