我想在加载时返回指向网页的链接列表。现在我有一个名为 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?
编辑:这是调试时错误的屏幕截图。
最佳答案
空引用异常看起来像是来自 ssoList.Links
正在 null
调用AddRange
时在它上面,所以它需要初始化为 List<SsoLink>
的新实例在调用 AddRange()
之前.
关于c# - 用列表分离模型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61718195/