c# - 用列表分离模型

标签 c# elasticsearch asp.net-core-mvc nest

我想在加载时返回指向网页的链接列表。现在我有一个名为 SsoLink.cs 的模型绑定(bind)到页面。我想返回一个列表,所以我创建了另一个名为 SsoLinks.cs 的模型,它有一个列表。在我的辅助函数中,我不断收到“对象未设置为对象的实例”。

SsoLink.cs

public class SsoLink
{
    public enum TypesOfLinks
    {
        [Display(Name="Please Select a Type")]
        Types,
        Collaboration,
        [Display(Name="Backups & Storage")]
        Backups_Storage,
        Development,
        [Display(Name="Cloud Services")]
        Cloud_Services,
        [Display(Name="Human Resources")]
        Human_Resources,
        Analytics
    }

    public string Id { get; set; }
    public string Name { get; set; }
    public string Url { get; set; }
    public string Owner { get; set; }
    public string OwnerEmail { get; set; }
    public string LinkDescription { get; set; }
    public TypesOfLinks LinkType { get; set; }
}

SsoLinks.cs
public class SsoLinks
{
    public List<SsoLink> Links {get; set;}
}

GetLinksHelper.cs
public partial class SsoLinkHelper
    {
        public static SsoLinks GetLinks()
        {
            var ssoList = new SsoLinks();
            try
            {
                //search the index for all sso entries
                var searchResponse = _client.Search<SsoLink>(s => s
                    .Index(_ssoLinkIndex)
                    .Size(500)
                    .Query(q => q
                        .MatchAll()
                    )
                );
                if (searchResponse.Documents.Count == 0)
                {
                    return ssoList;
                }
                ssoList.Links.AddRange(searchResponse.Hits.Select(hit => new SsoLink() {Id = hit.Source.Id, Name = hit.Source.Name, Url = hit.Source.Url, Owner = hit.Source.Owner}));
                return ssoList;
            }
            catch (Exception e)
            {
                Log.Error(e, "Web.Helpers.SsoLinkHelper.GetLinks");
                return ssoList;
            }
        }
    }

在调试时,它在 SsoLinks.Links.AddRange(etc) 处失败。如何为查询中找到的每个项目向 ssoList 添加新的 SsoLink?

编辑:这是调试时错误的屏幕截图。
SSOError

最佳答案

空引用异常看起来像是来自 ssoList.Links正在 null调用AddRange时在它上面,所以它需要初始化为 List<SsoLink> 的新实例在调用 AddRange() 之前.

关于c# - 用列表分离模型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61718195/

相关文章:

c# - 格式化

c# - 哪些API路由设计符合最佳实践?

c# - 使用 ninject 进行 WinForms 对话框的最佳实践是什么?

spring-mvc - ES Spring数据将大小添加到spring数据格式中

c# - .NET Core DI 中的异步提供程序

c# - 路由添加静态段让分页更友好asp.net core

c# - 任何时候访问类属性(获取或设置)时执行的方法?

arrays - Elasticsearch中的排序数组类型

elasticsearch - 默认情况下,Elasticsearch 7.5.2集群仅为索引创建一个分片

c# - 如何将具有动态值的 javascript 对象发布到 mvc6 Controller 操作