在 ASP.NET MVC 中创建混合模型(与其他模型的位)?

发布于 2024-08-22 06:24:46 字数 1599 浏览 6 评论 0原文

我为 ASP.NET MVC 应用程序创建了一个审核表来跟踪关键更改和操作。我想在视图上以易于阅读的格式向授权用户呈现该表的内容。

审核表(简化)如下所示:

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

到目前为止,我刚刚介绍了在 MVC 中使用默认的“列表”视图,但是我们即将结束开发,我想稍微整理一下这个视图通过显示员工姓名而不是 StaffID。

最初,我使用创建一个包含审计和员工的“混合模型”的方法,并将其传递给视图:

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

但这会导致视图混乱:

<%
foreach (var Entry in Model.AuditEntries)
{
  var StaffDetails = Model.AllStaff.Where(s => s.ID == Entry.StaffID).SingleOrDefault();
  /* output HTML */
}
%>

所以我想我想做的是创建一个新模型具有以下属性:

  • ID (int) - Audit.ID
  • StaffName (string) - Staff.ID [s => s.StaffID == Staff.ID]
  • Action (string) - Audit.Action
  • Timestamp (datetime) - Audit.Timestamp

但我想在控制器中执行此操作并将其作为 ToList() 传递到视图,以便我的视图可以是更干净、更简单。

有什么建议吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

回眸一遍 2024-08-29 06:24:46

您可能需要考虑在创建审核记录时填写员工的 ID 和姓名,当然,除非您从未对员工数据进行任何删除。我通常这样做是因为我的人员来源是在我们的企业目录中外部维护的。将完整数据保留在审计表中意味着您不必组合查询来获取所需内容,并且即使执行操作的人员离开公司并且他们的数据从员工表中删除,您的审计数据也完好无损。您唯一需要担心的是名称更改,这就是为什么我保留 id 和名称,以便在必要时可以修复名称。

You might want to consider filling both the id and name of the staff member at the time that the audit record is created, unless of course, you never do any deletes on your staff data. I typically do this because my source of people is maintained externally in our enterprise directory. Keeping the full data in the audit table means that you don't have to combine queries to get what you want and your audit data is intact even if the person doing the action leaves the company and their data is removed from the staff table. The only thing you need to worry about is name changes, which is why I keep both the id and the name so the name can be fixed up if necessary.

凉月流沐 2024-08-29 06:24:46

好吧,所以我开始冒险并尝试自己解决这个问题,我已经找到了解决方案,但我不确定它有多正确!

因此,首先,我创建了一个新类:

public class AuditEntries
{
  public int ID { get; set; }
  public string StaffName { get; set; }
  public string Action { get; set; }
  public System.DateTime Timestamp { get; set; }
}

然后,我将 ViewAuditTrail 操作重构为如下所示:

[AcceptVerbs("GET")]
public ActionResult ViewAuditTrail()
{
  var Audit = (from a in db.Audits orderby a.Timestamp descending select a).ToList();

  var AuditEntry = new List<AuditEntries>();

  foreach (var e in Audit)
  {
    var Staff = (from s in db.Staffs where s.ID == e.StaffID select s).SingleOrDefault();

    AuditEntries Entry = new AuditEntries();
    Entry.ID = e.ID;
    Entry.StaffName = Staff.Forename + " " + Staff.Surname + " (" + Staff.ID.ToString() + ")";
    Entry.Action = e.Action;
    Entry.Timestamp = e.Timestamp;

    AuditEntry.Add(Entry);
  }

  return View(AuditEntry.ToList());
}

现在,我的 ViewAuditTrail 视图只是一个使用默认 MVC 列表视图类型绑定到 AuditEntries 类的强类型视图。

它似乎工作正常,但我确信它可以改进 - 有人有任何建议吗?

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:

public class AuditEntries
{
  public int ID { get; set; }
  public string StaffName { get; set; }
  public string Action { get; set; }
  public System.DateTime Timestamp { get; set; }
}

I then refactored my ViewAuditTrail action to look like so:

[AcceptVerbs("GET")]
public ActionResult ViewAuditTrail()
{
  var Audit = (from a in db.Audits orderby a.Timestamp descending select a).ToList();

  var AuditEntry = new List<AuditEntries>();

  foreach (var e in Audit)
  {
    var Staff = (from s in db.Staffs where s.ID == e.StaffID select s).SingleOrDefault();

    AuditEntries Entry = new AuditEntries();
    Entry.ID = e.ID;
    Entry.StaffName = Staff.Forename + " " + Staff.Surname + " (" + Staff.ID.ToString() + ")";
    Entry.Action = e.Action;
    Entry.Timestamp = e.Timestamp;

    AuditEntry.Add(Entry);
  }

  return View(AuditEntry.ToList());
}

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?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文