Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/281.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/36.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何在ASP.NET中返回视图中的空列表_C#_Asp.net_Controller_Actionresult - Fatal编程技术网

C# 如何在ASP.NET中返回视图中的空列表

C# 如何在ASP.NET中返回视图中的空列表,c#,asp.net,controller,actionresult,C#,Asp.net,Controller,Actionresult,我想显示具有指定角色名的所有用户。我所做的只是在视图中指定一个角色名。如果此名称存在,则显示所有相关用户(现在可以使用),或不显示任何内容(此处出现异常)。 这是我在控制器中的代码: public ActionResult ViewUser(string roleName) { var UsersContext = new ApplicationDbContext(); var roleManager = new RoleManager<

我想显示具有指定角色名的所有用户。我所做的只是在视图中指定一个角色名。如果此名称存在,则显示所有相关用户(现在可以使用),或不显示任何内容(此处出现异常)。 这是我在控制器中的代码:

public ActionResult ViewUser(string roleName)
    {       
        var UsersContext = new ApplicationDbContext();
        var roleManager = new RoleManager<IdentityRole>(new RoleStore<IdentityRole>(new ApplicationDbContext()));
        if (roleManager.RoleExists(roleName))
        {
            var role = roleManager.FindByName(roleName).Users.First();
            var usersInRole = UsersContext.Users.Where(u => u.Roles.Select(r => r.RoleId).Contains(role.RoleId)).ToList();
            return View(usersInRole);
        }
        else
        {
            return View();
        }

    }
@Html.ActionLink("Manage User", "ViewUser", "Home", new { @roleName="Worker"}, new { style = "color:white" })
下面的屏幕截图是我指定数据库中的“Customer”作为角色名时的结果。如果我指定了另一个不存在的名称,则结果应该不包含任何用户列表。

我希望我能在这里为您举行婚礼。。。。。 您希望返回一个空列表,可以通过以下方法实现:

private List<myClass> GetList(){
        List<myClass> list = new List<myClass>();
        list.Add(new myClass());   // Add your Class
        return list;               // retunrs a empty List
}
private List GetList(){
列表=新列表();
list.Add(new myClass());//添加您的类
返回列表;//返回空列表
}
公共操作结果视图用户(字符串roleName)
{       
var UsersContext=new ApplicationDbContext();
var rolemanger=new rolemanger(new RoleStore(new ApplicationDbContext());
if(roleManager.RoleExists(roleName))
{
var role=rolemager.FindByName(roleName.Users.First();
var usersInRole=UsersContext.Users.Where(u=>u.Roles.Select(r=>r.RoleId).Contains(role.RoleId)).ToList();
返回视图(usersInRole);
}
其他的
{
返回视图(新列表());
}
}

假设您从
usersInRole
集合返回
if
块中的
User
实体实例,您可以重新构造
if
块,在不满足任何条件的情况下自动返回空
列表
集合(还添加了
null
检查
FindByName()
方法):


此外,请确保您正在使用
@model User
在查看页面中绑定它。

“此处出现异常”-哪种异常?另外,您希望获得什么样的预期结果?用户角色的数据类型是什么?
var-role=rolemager.FindByName(roleName.Users.First()将引发异常。考虑在查询中使用<代码> FrStReDebug()/<代码>,因为它将返回默认值(在您的情况下为NULL),如果没有找到。非常感谢。但这里的用户是什么?我没有任何名为User的类
User
是与
Users
实体相关的示例类名,实际类名取决于
UsersContext.Users
查询返回的内容。
public ActionResult ViewUser(string roleName)
    {       
        var UsersContext = new ApplicationDbContext();
        var roleManager = new RoleManager<IdentityRole>(new RoleStore<IdentityRole>(new ApplicationDbContext()));
        if (roleManager.RoleExists(roleName))
        {
            var role = roleManager.FindByName(roleName).Users.First();
            var usersInRole = UsersContext.Users.Where(u => u.Roles.Select(r => r.RoleId).Contains(role.RoleId)).ToList();
            return View(usersInRole);
        }
        else
        {
            return View(new List<Users>());
        }

    }
public ActionResult ViewUser(string roleName)
{       
    var UsersContext = new ApplicationDbContext();
    var usersInRole = new List<User>(); // assign instance before if conditions
    var roleManager = new RoleManager<IdentityRole>(new RoleStore<IdentityRole>(new ApplicationDbContext()));

    if (roleManager.RoleExists(roleName))
    {
        // change to FirstOrDefault is more recommended
        var role = roleManager.FindByName(roleName).Users.FirstOrDefault();
        if (role != null)
        {
            usersInRole = UsersContext.Users.Where(u => u.Roles.Select(r => r.RoleId).Contains(role.RoleId)).ToList();
        }
    }

    return View(usersInRole);
}
var emptyList = new List<User>();
return View(emptyList);