我的项目中有多个模型,但在下面的屏幕中,大多数字段/属性都位于 SecurityLog 模型中。
下面是我显示连接的军官列表的地方。除了官员姓名之外,我的搜索和列标题排序可以正常工作。我很难合并官员姓名 b/c 列表来自另一个页面模型。
这是我的数据库架构和示例结果
我可以根据 Microsoft 的 Contoso 大学的演示来实现排序、搜索和分页。
https://docs.microsoft.com/en-us/aspnet/core/data/ef-rp/sort-filter-page?view=aspnetcore-3.1
我如何解决在我当前代码中搜索官员姓名的问题?更具体地说,对于搜索……我如何阅读(迭代)OfficerID 列表并搜索每个列表项(连接的官员列表行)的字符串值?
foreach (SecurityLog secLog in SecurityLog)
{
secLogCopy = secLog;
OfficerLists = officerList.GetOfficerList(_context, secLog, rowID, OfficerIDs);
if (!String.IsNullOrEmpty(searchString))
{
sort = sort.Where(s => OfficerIDs.ToString().Contains(searchString));
}
rowID++;
}
Run Code Online (Sandbox Code Playgroud)
页面模型:
namespace SecurityCore.Pages.SecurityLogs
{
public class IndexModel : PageModel
{
private readonly SecurityCore.Models.SecurityCoreContext _context;
public IndexModel(SecurityCore.Models.SecurityCoreContext context)
{
_context = context;
}
public string EventDateSort { get; set; }
public string CurrentSort { get; set; }
[DataType(DataType.Date)]
public Nullable<DateTime> …Run Code Online (Sandbox Code Playgroud) 在这里,我可以在创建页面上绑定多选选择列表。
<select id="multiple" asp-for="SecurityLog.Officer" multiple="multiple" class="selectpicker form-control" asp-items="ViewBag.Officer">
</select>
public IActionResult OnGetAsync()
{
ViewData["Officer"] = new SelectList(_context.Officer.Where(a => a.Active == "Y"), "ID", "FullName");
return Page();
}
Run Code Online (Sandbox Code Playgroud)
我遇到的最大问题是我正在尝试编辑/发布值,但我的模型中没有可以绑定的直接字段。
我在这里找到了一个很好的例子,但它只向您展示如何保存 1 个选择。 https://www.learnrazorpages.com/razor-pages/forms/select-lists
我的 SecurityLog 模型有这个
public virtual Officer Officer { get; set; }
Run Code Online (Sandbox Code Playgroud)
我的军官模型有这个
namespace SecurityCore.Models
{
public class Officer
{
[Required]
public int ID { get; set; }
[Required]
[Display(Name = "Officer's First Name")]
public string FirstName { get; set; }
[Required]
[Display(Name = "Officer's Last Name")]
public string LastName …Run Code Online (Sandbox Code Playgroud)