小编Rob*_*b C的帖子

如何搜索来自另一个页面模型的串联名称列表?

我的项目中有多个模型,但在下面的屏幕中,大多数字段/属性都位于 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)

c# asp.net-core razor-pages

5
推荐指数
2
解决办法
336
查看次数

无法在选择列表中保存多个选择 - asp.net core web 应用程序 razor 页面

在这里,我可以在创建页面上绑定多选选择列表。

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

c# sql razor asp.net-core razor-pages

2
推荐指数
1
解决办法
4604
查看次数

标签 统计

asp.net-core ×2

c# ×2

razor-pages ×2

razor ×1

sql ×1