Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/29.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# &引用;任务<;PeopleUser>;不包含角色和无扩展方法的定义“;角色“;接受Tasl类型的第一个参数<;PeopleUser>;_C#_Asp.net_Asp.net Mvc_Identity - Fatal编程技术网

C# &引用;任务<;PeopleUser>;不包含角色和无扩展方法的定义“;角色“;接受Tasl类型的第一个参数<;PeopleUser>;

C# &引用;任务<;PeopleUser>;不包含角色和无扩展方法的定义“;角色“;接受Tasl类型的第一个参数<;PeopleUser>;,c#,asp.net,asp.net-mvc,identity,C#,Asp.net,Asp.net Mvc,Identity,有人能帮我吗?我想在Identity 3中实现“编辑用户”角色,但在编写此文件时,出现了一个错误: 任务不包含角色的定义,并且没有 扩展方法“角色”接受类型为的第一个参数 任务 这是控制器代码 public virtual ActionResult Edit(PeopleUser user, string role) { if (ModelState.IsValid) { var oldUser = _userManager.FindByIdAsync(user

有人能帮我吗?我想在Identity 3中实现“编辑用户”角色,但在编写此文件时,出现了一个错误:

任务不包含角色的定义,并且没有 扩展方法“角色”接受类型为的第一个参数 任务

这是控制器代码

public virtual ActionResult Edit(PeopleUser user, string role)
{
    if (ModelState.IsValid)
    { 
        var oldUser = _userManager.FindByIdAsync(user.Id);
        var oldRoleId = oldUser.Roles.SingleOrDefault().RoleId;
        var oldRoleName = _db.Roles.SingleOrDefault(r => r.Id == oldRoleId).Name;

        if (oldRoleName != role)
        {
            _userManager.RemoveFromRoleAsync(user, oldRoleName);
            _userManager.AddToRoleAsync(user, role);
        }
        _db.Entry(user).State = EntityState.Modified;

        return RedirectToAction("Index", "Home");
    }
    return View(user);
}

在使用
TUser
对象之前,您需要等待
任务

// Note that the signature has async and Task<ActionResult>.
public virtual async Task<ActionResult> Edit(PeopleUser user, string role)
{
    if (ModelState.IsValid)
    { 
        var oldUser = await _userManager.FindByIdAsync(user.Id); // Note the await keyword.
        var oldRoleId = oldUser.Roles.SingleOrDefault().RoleId;
        var oldRoleName = _db.Roles.SingleOrDefault(r => r.Id == oldRoleId).Name;

        if (oldRoleName != role)
        {
            // I've added await here as well...
            await _userManager.RemoveFromRoleAsync(user, oldRoleName);
            await _userManager.AddToRoleAsync(user, role);
        }
        _db.Entry(user).State = EntityState.Modified;

        return RedirectToAction("Index", "Home");
    }
    return View(user);
}
为了澄清你的例子:

// Create the task. Note the difference between the types of oldUserTask and oldUser.
Task<User> oldUserTask = _userManager.FindByIdAsync(user.Id);
// Await the task.
User oldUser = await oldUserTask;
// Now you can do something with oldUser.
oldUser.DoStuff();
//创建任务。请注意oldUserTask和oldUser类型之间的差异。
Task oldUserTask=\u userManager.FindByIdAsync(user.Id);
//等待任务。
用户oldUser=等待oldUserTask;
//现在,您可以对oldUser执行一些操作。
oldUser.DoStuff();
更多关于异步编程的信息

在.NET Framework编程中,异步方法通常返回
任务
任务
。在异步方法中,使用
await
运算符 应用于从调用另一个异步方法返回的任务。 如果方法包含 返回语句,指定类型为
TResult
的操作数。你用 如果方法没有return语句或具有 不返回操作数的return语句


您使用什么.Net平台,使用语句和程序集引用?使用EF 7的core 1和使用Microsoft.AspNet.Authorization的Identity 3;使用Microsoft.AspNet.Identity;使用Microsoft.AspNet.Mvc;使用Microsoft.Data.Entity;使用制度;使用System.Linq;使用System.Threading.Tasks;使用交通模型;非常感谢,它可以工作,但我有一些问题,因为当我在user founded的角色中进行调试时,我看到null,但在接口中,当我编写razor语法@user.isinrole时…它可以工作,并且在表ASP.NetUsers中出现
// Create the task. Note the difference between the types of oldUserTask and oldUser.
Task<User> oldUserTask = _userManager.FindByIdAsync(user.Id);
// Await the task.
User oldUser = await oldUserTask;
// Now you can do something with oldUser.
oldUser.DoStuff();