Asp.net core 删除ActionResult.NETCore

Asp.net core 删除ActionResult.NETCore,asp.net-core,.net-core,Asp.net Core,.net Core,我得到错误“运算符”==”不能应用于此行的“字符串”和“int”类型的操作数 .FirstOrDefaultAsync(m => m.Id == id); 到目前为止,除了这个方法,我什么都没试过 public class EditProfileController : Controller { private readonly ApplicationDbContext db; private object _context; public E

我得到错误“运算符”==”不能应用于此行的“字符串”和“int”类型的操作数

      .FirstOrDefaultAsync(m => m.Id == id);
到目前为止,除了这个方法,我什么都没试过

  public class EditProfileController : Controller
{

    private readonly ApplicationDbContext db;
    private object _context;

    public EditProfileController(ApplicationDbContext Db)
    {
        db = Db;
    }
    public IActionResult Index()
    {
      List<ApplicationUser> listdata = db.Users
        .Select(user => new ApplicationUser()
        {
            Id =  user.Id,
            UserName = user.UserName,
            NormalizedUserName = user.NormalizedUserName,
                            Email = user.Email


            // Add the remainder properties
        }).ToList();
    return View(listdata);
    }
    [HttpGet]
    public ActionResult Edit(string id)
    {
        //Get user and return the Edit View
        ApplicationUser user = db.Users.Where(u => u.Id == id)
      .Select(u => new ApplicationUser()
      {
          Id = u.Id,
          UserName = u.UserName,
          NormalizedUserName = u.NormalizedUserName,
          Email = u.Email

          // Add the remainder properties
      }).FirstOrDefault();
        return View(user);
    }


    [HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult Edit(ApplicationUser listdata)
    {
        if (ModelState.IsValid)
        {
            db.Entry(listdata).State = EntityState.Modified;
            db.SaveChanges();
            return RedirectToAction("Index");
        }
        return View(listdata);
    }


    public async Task<IActionResult> Delete(int? id)
    {
        if (id == null)
        {
            return NotFound();
        }

        var user = await db.Users
            .FirstOrDefaultAsync(m => m.Id == id);
        if (user == null)
        {
            return NotFound();
        }

        return View(user);
    }


    [HttpPost, ActionName("Delete")]
    [ValidateAntiForgeryToken]
    public async Task<IActionResult> DeleteConfirmed(int id)
     {
        var user = await db.Users.FindAsync(id);
        db.Users.Remove(user);
        await db.SaveChangesAsync();
        return RedirectToAction(nameof(Index));
     }
  }
公共类EditProfileController:控制器
{
私有只读ApplicationDbContext数据库;
私有对象(上下文);
公共EditProfileController(ApplicationDbContext数据库)
{
db=db;
}
公共IActionResult索引()
{
List listdata=db.Users
.Select(用户=>newapplicationuser()
{
Id=user.Id,
UserName=user.UserName,
NormalizedUserName=user.NormalizedUserName,
Email=user.Email
//添加其余属性
}).ToList();
返回视图(listdata);
}
[HttpGet]
公共操作结果编辑(字符串id)
{
//获取用户并返回编辑视图
ApplicationUser user=db.Users.Where(u=>u.Id==Id)
.Select(u=>newapplicationuser()
{
Id=u.Id,
用户名=u.UserName,
NormalizedUserName=u.NormalizedUserName,
电子邮件=美国电子邮件
//添加其余属性
}).FirstOrDefault();
返回视图(用户);
}
[HttpPost]
[ValidateAntiForgeryToken]
公共操作结果编辑(ApplicationUser listdata)
{
if(ModelState.IsValid)
{
db.Entry(listdata).State=EntityState.Modified;
db.SaveChanges();
返回操作(“索引”);
}
返回视图(listdata);
}
公共异步任务删除(int?id)
{
if(id==null)
{
返回NotFound();
}
var user=await db.Users
.FirstOrDefaultAsync(m=>m.Id==Id);
if(user==null)
{
返回NotFound();
}
返回视图(用户);
}
[HttpPost,ActionName(“删除”)]
[ValidateAntiForgeryToken]
公共异步任务删除已确认(int-id)
{
var user=await db.Users.FindAsync(id);
db.Users.Remove(用户);
等待db.saveChangesSync();
返回重定向到操作(名称(索引));
}
}
我的预期结果是,当我单击查看页面上的“删除”链接时,它会要求确认删除,然后单击“是”或“否”,它会执行“删除确认”操作。我需要什么来修复此错误?

删除(int?id)
更改为
删除(字符串id)
删除确认(int id)
更改为
删除确认(字符串id)
,如下所示:

public async Task<IActionResult> Delete(string id) // <-- Here it is
{
    if (id == null)
    {
        return NotFound();
    }

    var user = await db.Users.FirstOrDefaultAsync(m => m.Id == id);
    if (user == null)
    {
        return NotFound();
    }

    return View(user);
}

[HttpPost, ActionName("Delete")]
[ValidateAntiForgeryToken]
public async Task<IActionResult> DeleteConfirmed(string id) // <-- Here it is
{
    var user = await db.Users.FindAsync(id);
    db.Users.Remove(user);
    await db.SaveChangesAsync();
    return RedirectToAction(nameof(Index));
}
公共异步任务删除(字符串id)//m.id==id); if(user==null) { 返回NotFound(); } 返回视图(用户); } [HttpPost,ActionName(“删除”)] [ValidateAntiForgeryToken] 公共异步任务删除确认(字符串id)/更改
删除(字符串id)
删除(字符串id)
删除确认(字符串id)
删除确认(字符串id)
,如下所示:

public async Task<IActionResult> Delete(string id) // <-- Here it is
{
    if (id == null)
    {
        return NotFound();
    }

    var user = await db.Users.FirstOrDefaultAsync(m => m.Id == id);
    if (user == null)
    {
        return NotFound();
    }

    return View(user);
}

[HttpPost, ActionName("Delete")]
[ValidateAntiForgeryToken]
public async Task<IActionResult> DeleteConfirmed(string id) // <-- Here it is
{
    var user = await db.Users.FindAsync(id);
    db.Users.Remove(user);
    await db.SaveChangesAsync();
    return RedirectToAction(nameof(Index));
}
公共异步任务删除(字符串id)//m.id==id); if(user==null) { 返回NotFound(); } 返回视图(用户); } [HttpPost,ActionName(“删除”)] [ValidateAntiForgeryToken]
public async Task deleteComfixed(string id)//错误很明显,并指出代码中存在问题。
id
是字符串还是整数?不可能两者都有。使用正确的类型。
IdentityUser
的默认主键列类型是字符串,而不是int。如果愿意,可以将其设置为int,但这不是默认值。另外,FWIW,您应该始终使用
UserManager
管理用户,而不是像现在这样直接使用上下文。在创建用户时,它会在幕后做一些你的应用程序代码不应该关心的事情(比如规范用户名和电子邮件)。您可以通过上下文选择用户,但标准CRUD应该通过管理器进行。错误很明显,并指出代码中存在问题。
id
是字符串还是整数?不可能两者都有。使用正确的类型。
IdentityUser
的默认主键列类型是字符串,而不是int。如果愿意,可以将其设置为int,但这不是默认值。另外,FWIW,您应该始终使用
UserManager
管理用户,而不是像现在这样直接使用上下文。在创建用户时,它会在幕后做一些你的应用程序代码不应该关心的事情(比如规范用户名和电子邮件)。您可以通过上下文选择用户,但标准CRUD应该通过管理器进行。