【发布时间】:2019-06-26 03:32:06
【问题描述】:
我的目标是让用户根据特定条件(名字、姓氏、ID)搜索现有用户的广告并显示匹配的项目。
我正在使用 PrincipalSearcher 来完成此操作:
控制器:
public ViewResult Index(string fname, string lname, int id)
{
UserPrincipal user = new UserPrincipal(pcontext);
user.GivenName = fname;
user.Surname = lname;
user.EmployeeId = id.ToString();
PrincipalSearcher ps = new PrincipalSearcher();
ps.QueryFilter = user;
PrincipalSearchResult<Principal> results = ps.FindAll();
List<Principal> userResults = new List<Principal>();
foreach (Principal item in results)
{
userResults.Add(item);
}
var viewModel = new UserSearchViewModel
{
Users = userResults.ToList()
};
return View(viewModel);
}
最后,我将结果传递给我创建的视图模型,并将其绑定到列表模型。
视图模型:
public class UserSearchViewModel
{
public Request Request { get; set; }
public List<UserSearch> Users { get; set; }
public int id { get; set; }
public string fname { get; set; }
public string lname { get; set; }
}
我得到的当前错误消息如下: 无法将类型“System.Collections.Generic.List”隐式转换为“System.Collections.Generic.List”
我尝试将 userResults 更改为 List 类型,但错误仍然存在。我尝试只返回结果,但不知道如何提交用户结果,然后在视图中显示结果。
对实现我的目标的任何帮助或指导将不胜感激。
编辑:模型
public class UserSearch
{
public int employeeId { get; set; }
public string firstname { get; set; }
public string lastname { get; set; }
}
【问题讨论】:
-
UserSearch类型是你的,对吧?可以展示一下吗? -
@octavioccl - 我已经添加了模型,真的很基本。
-
是的,那你需要投影
userresult集合,我加个答案
标签: c# asp.net-mvc asp.net-core