DNN 9.1.1/2sxc 9.14:实体信息获取及Razor模板展示问询
Absolutely! In DNN 9.1.1 paired with 2sxc 9.14, all the metadata and history you're asking about is fully accessible in your Razor templates. Let's walk through each scenario with practical code examples:
1. 获取实体创建者与最后修改者的ID/名称
2sxc entities automatically track creator and modifier details via built-in properties on the IEntity object. Here's how to pull that data:
创建者信息
- Creator User ID: Use the
CreatedByproperty (returns an integer ID):@{ // Replace with your content type and entity ID, or use @Content for the current item var targetEntity = App.Data["BlogPost"].GetOne(456); var creatorId = targetEntity.CreatedBy; } Creator User ID: @creatorId - Creator Display Name: Fetch the full DNN UserInfo object to get a human-readable name:
@using DotNetNuke.Entities.Users @{ var creatorUser = UserController.GetUserById(PortalSettings.PortalId, targetEntity.CreatedBy); var creatorName = creatorUser?.DisplayName ?? "Unknown User"; } Created by: @creatorName
最后修改者信息
- Modifier User ID: Use the
ModifiedByproperty (works the same way asCreatedBy):var modifierId = targetEntity.ModifiedBy; Last Modified By ID: @modifierId - Modifier Display Name: Fetch the user details just like with the creator:
var modifierUser = UserController.GetUserById(PortalSettings.PortalId, targetEntity.ModifiedBy); var modifierName = modifierUser?.DisplayName ?? "Unknown User"; Last modified by: @modifierName
2. 获取实体的创建与更新时间
These are native DateTime properties on every 2sxc entity, so you can format them directly:
<p>Created on: @targetEntity.Created.ToString("yyyy-MM-dd HH:mm")</p> <p>Last updated on: @targetEntity.Modified.ToString("yyyy-MM-dd HH:mm")</p>
3. 获取实体历史记录
2sxc 9.14 includes a History property on the IEntity object, which returns a collection of IEntityHistoryItem entries. Each entry tracks who made changes, when, and exactly which fields were updated.
Here's an example of rendering a readable history log:
@using DotNetNuke.Entities.Users <h3>Entity Change History</h3> @if (targetEntity.History.Any()) { <ul class="entity-history"> @foreach (var change in targetEntity.History.OrderByDescending(c => c.Timestamp)) { var changeUser = UserController.GetUserById(PortalSettings.PortalId, change.UserId); var userName = changeUser?.DisplayName ?? "Unknown User"; <li> <strong>@userName</strong> | @change.Timestamp.ToString("yyyy-MM-dd HH:mm")<br> Modified fields: @string.Join(", ", change.ChangedFields.Select(f => f.FieldName)) </li> } </ul> } else { <p>No change history found for this entity.</p> }
Quick Tip for Content Templates
If you're working directly with the current content item in a template (e.g., a blog post detail view), you can skip fetching the entity and use @Content directly:
<p>Created by: @UserController.GetUserById(PortalSettings.PortalId, Content.CreatedBy)?.DisplayName</p> <p>Created on: @Content.Created.ToString("MMM dd, yyyy")</p>
内容的提问来源于stack exchange,提问作者Jernej Pirc

