【发布时间】:2020-01-30 00:21:49
【问题描述】:
在我的主索引页面中,我想在军官姓名列中显示军官列表。即 Tommy Bahamas、Clinton K 等......
我遇到了困难,因为在索引页面上,Officer 和 SecurityLogOfficer 表不是我的 SecurityLog 模型的一部分。
安全日志索引页面
@foreach (var item in Model.SecurityLog)
{
<tr>
<td style="width:4% !important">
@Html.DisplayFor(modelItem => item.ID)
</td>
<td style="width:5% !important">
@Html.DisplayFor(modelItem => item.EventDate)
</td>
<td style="width:5% !important">
@Html.DisplayFor(modelItem => item.OfficerList)
</td>
}
我创建了一个类,OfficerList,以返回逗号分隔的军官列表,但我无法在 SecurityLog 索引页面上显示它。
public class OfficerList : SecurityLog
{
private readonly SecurityCore.Models.SecurityCoreContext _context;
public OfficerList(SecurityCore.Models.SecurityCoreContext context)
{
_context = context;
}
public List<string> GetOfficerList()
{
List<string> OfficerIDs = new List<string>();
//use the syntax .ToList() to convert object read from db to list to avoid being re-read again
var SecLog = _context.SecurityLog.ToList();
var SecLogOfficer = _context.SecurityLogOfficer.ToList();
var Officer = _context.Officer.ToList();
int rowID;
//string[] OfficerIDs = new string[100];
rowID = 0;
foreach (SecurityLog sl in SecLog)
{
foreach (SecurityLogOfficer slo in SecLogOfficer.Where(slo => slo.SecurityLogID == sl.ID))
{
if (OfficerIDs[rowID] == null)
{
OfficerIDs[rowID] = slo.Officer.FullName + ", ";
}
else
{
OfficerIDs[rowID] = OfficerIDs[rowID] + slo.Officer.FullName + ", ";
}
}
rowID++;
}
return OfficerIDs;
}
}
这是我想显示串联列表的地方...
这是我的数据库架构和示例结果
我们将不胜感激。谢谢!
更新
在我的 SecurityLog 模型中检索串联名称列表时遇到问题。
public class SecurityLog
{
private readonly SecurityCore.Models.SecurityCoreContext _context;
public SecurityLog(SecurityCore.Models.SecurityCoreContext context)
{
_context = context;
}
......
public List<string> OfficerList
{
get
{
var officerList = new OfficerList(_context);
return officerList.GetOfficerList();
}
}
}
修改了OfficerList类
public class OfficerList : SecurityLog
{
private readonly SecurityCore.Models.SecurityCoreContext _context;
public OfficerList(SecurityCoreContext context) : base(context)
{
_context = context;
}
public List<string> GetOfficerList()
{
List<string> OfficerIDs = new List<string>();
//use the syntax .ToList() to convert object read from db to list to avoid being re-read again
var SecLog = _context.SecurityLog.ToList();
var SecLogOfficer = _context.SecurityLogOfficer.ToList();
var Officer = _context.Officer.ToList();
int rowID;
//string[] OfficerIDs = new string[100];
rowID = 0;
foreach (SecurityLog sl in SecLog)
{
foreach (SecurityLogOfficer slo in SecLogOfficer.Where(slo => slo.SecurityLogID == sl.ID))
{
if (OfficerIDs[rowID] == null)
{
OfficerIDs[rowID] = slo.Officer.FullName + ", ";
}
else
{
OfficerIDs[rowID] = OfficerIDs[rowID] + slo.Officer.FullName + ", ";
}
}
rowID++;
}
return OfficerIDs;
}
}
我的 SecurityLog _context 说它是空的。
SqlNullValueException:数据为空。此方法或属性不能 调用 Null 值。 Microsoft.Data.SqlClient.SqlBuffer.ThrowIfNull() Microsoft.Data.SqlClient.SqlBuffer.get_DateTime() Microsoft.Data.SqlClient.SqlDataReader.GetDateTime(int i) lambda_method(闭包,QueryContext,DbDataReader,ResultContext, int[] , ResultCoordinator ) Microsoft.EntityFrameworkCore.Query.Internal.QueryingEnumerable+Enumerator.MoveNext() System.Collections.Generic.List..ctor(IEnumerable 集合) System.Linq.Enumerable.ToList(IEnumerable 源) OfficeList.cs 中的 SecurityCore.Models.OfficerList.GetOfficerList() + var SecLog = _context.SecurityLog.ToList(); SecurityLog.cs 中的 SecurityCore.Models.SecurityLog.get_OfficerList() + 返回officerList.GetOfficerList(); Microsoft.AspNetCore.Mvc.ViewFeatures.ExpressionMetadataProvider+c__DisplayClass0_0.g__modelAccessor|0(对象容器) Microsoft.AspNetCore.Mvc.ViewFeatures.ModelExplorer.get_Model() Microsoft.AspNetCore.Mvc.ViewFeatures.TemplateBuilder..ctor(IViewEngine viewEngine, IViewBufferScope bufferScope, ViewContext viewContext, ViewDataDictionary viewData, ModelExplorer modelExplorer, string htmlFieldName, string templateName, bool readOnly, object 附加视图数据) Microsoft.AspNetCore.Mvc.ViewFeatures.HtmlHelper.GenerateDisplay(ModelExplorer modelExplorer,字符串 htmlFieldName,字符串 templateName,对象 附加视图数据) Microsoft.AspNetCore.Mvc.ViewFeatures.HtmlHelper.DisplayFor(Expression> 表达式,字符串模板名称,字符串 htmlFieldName, 对象附加视图数据) Microsoft.AspNetCore.Mvc.Rendering.HtmlHelperDisplayExtensions.DisplayFor(IHtmlHelper htmlHelper, Expression> 表达式) SecurityCore.Pages.SecurityLogs.Pages_SecurityLogs_Index.ExecuteAsync() 在 Index.cshtml 中 + @Html.DisplayFor(modelItem => item.OfficerList) Microsoft.AspNetCore.Mvc.Razor.RazorView.RenderPageCoreAsync(IRazorPage 页面,ViewContext 上下文) Microsoft.AspNetCore.Mvc.Razor.RazorView.RenderPageAsync(IRazorPage 页面,ViewContext 上下文,bool invokeViewStarts) Microsoft.AspNetCore.Mvc.Razor.RazorView.RenderAsync(ViewContext 语境) Microsoft.AspNetCore.Mvc.ViewFeatures.ViewExecutor.ExecuteAsync(ViewContext viewContext, 字符串 contentType, Nullable statusCode) Microsoft.AspNetCore.Mvc.ViewFeatures.ViewExecutor.ExecuteAsync(ViewContext viewContext, 字符串 contentType, Nullable statusCode) Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.g__Awaited|29_0(ResourceInvoker 调用程序,Task lastTask,State next, 范围作用域,对象状态,bool isCompleted) Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.Rethrow(ResultExecutedContextSealed 语境) Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.ResultNext(ref State next, ref Scope scope, ref object state, ref 布尔已完成) Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.InvokeResultFilters() Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.g__Awaited|24_0(ResourceInvoker 调用者,任务 lastTask,下一个状态,作用域范围,对象状态,布尔 完成了) Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.Rethrow(ResourceExecutedContextSealed 语境) Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.Next(参考状态 接下来,ref Scope 范围,ref 对象状态,ref bool isCompleted) Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.g__Awaited|19_0(ResourceInvoker 调用者,任务 lastTask,下一个状态,作用域范围,对象状态,布尔 完成了) Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.g__Logged|17_1(ResourceInvoker 调用者) Microsoft.AspNetCore.Routing.EndpointMiddleware.g__AwaitRequestTask|6_0(端点 端点、任务 requestTask、ILogger 记录器) Microsoft.AspNetCore.Authorization.AuthorizationMiddleware.Invoke(HttpContext 语境) Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware.Invoke(HttpContext 上下文)
更新 - 添加关系
public class SecurityCoreContext : DbContext
{
public SecurityCoreContext (DbContextOptions<SecurityCoreContext> options)
: base(options)
{
}
public DbSet<SecurityCore.Models.SecurityLog> SecurityLog { get; set; }
public DbSet<SecurityCore.Models.Entity> Entity { get; set; }
public DbSet<SecurityCore.Models.Location> Location { get; set; }
public DbSet<SecurityCore.Models.ShiftRange> ShiftRange { get; set; }
public DbSet<SecurityCore.Models.EventType> EventType { get; set; }
public DbSet<SecurityCore.Models.SecurityLogOfficer> SecurityLogOfficer { get; set; }
public DbSet<SecurityCore.Models.Officer> Officer { get; set; }
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<SecurityLogOfficer>()
.HasKey(t => new { t.SecurityLogID, t.OfficerID });
modelBuilder.Entity<SecurityLogOfficer>()
.HasOne(pt => pt.SecurityLog)
.WithMany(p => p.SecurityLogOfficers)
.HasForeignKey(pt => pt.SecurityLogID);
modelBuilder.Entity<SecurityLogOfficer>()
.HasOne(pt => pt.Officer)
.WithMany(t => t.SecurityLogOfficers)
.HasForeignKey(pt => pt.OfficerID);
}
}
2020 年 1 月 23 日更新
我尝试根据以下答案更新代码,但我认为这真的很接近解决;在 OfficeList 类中,目前没有 SecLog b/c 的值,在单击搜索按钮之前,页面加载上没有数据。
OfficerList officerList = new OfficerList();
OfficerLists = officerList.GetOfficerList(_context);
SecurityLog = await PaginatedList<SecurityLog>.CreateAsync(sort
.Include(a => a.Entity)
.Include(b => b.EventType)
.Include(c => c.Location)
.Include(d => d.ShiftRange)
.Include(e => e.Officer)
.AsNoTracking(), pageIndex ?? 1, pageSize);
进入OfficerList类时为空
var SecLog = _context.SecurityLog.ToList();
SqlNullValueException:数据为空。不能对 Null 值调用此方法或属性。 Microsoft.Data.SqlClient.SqlBuffer.ThrowIfNull() Microsoft.Data.SqlClient.SqlBuffer.get_DateTime() Microsoft.Data.SqlClient.SqlDataReader.GetDateTime(int i) lambda_method(闭包,QueryContext,DbDataReader,ResultContext, int[] , ResultCoordinator ) Microsoft.EntityFrameworkCore.Query.Internal.QueryingEnumerable+Enumerator.MoveNext() System.Collections.Generic.List..ctor(IEnumerable 集合) System.Linq.Enumerable.ToList(IEnumerable 源) SecurityCore.Models.OfficerList.GetOfficerList(SecurityCoreContext _context) 在OfficerList.cs + var SecLog = _context.SecurityLog.ToList(); SecurityCore.Pages.SecurityLogs.IndexModel.OnGetAsync(字符串 sortOrder,字符串 currentFilter,字符串 searchString,Nullable pageIndex、字符串 entitySelect、字符串 entityFilter、DateTime dateBegin, DateTime dateBeginSelect, DateTime dateEnd, DateTime index.cshtml.cs 中的 dateEndSelect) + 官员列表 = 官员列表.GetOfficerList(_context);
OfficerList 类中是否有办法不遍历 foreach 中的 SecurityLog 并以某种方式将 SecurityLog 的 ID 作为第二个参数传递给 OfficerList(连同 _context)?
【问题讨论】:
-
所以...对于一个 SecurityLogId 可以有多个 OfficerID 是否正确?
-
是的。没错。
-
那么您将拥有
item.OfficerList与 SecurityLogId 中的所有官员,然后只需迭代item.OfficerList集合并显示所有官员。 -
我仍将不得不尝试,但我意识到我在填充 SecurityLog 模型中的串联名称列表时遇到问题。我已经更新了我的代码并提供了返回的 null 错误。
-
你和实体有正常的关系吗?或者您可以制作一个示例并将其发布在 github 上并在此处分享链接,以便人们可以帮助您
标签: c# sql asp.net-core razor-pages