删除 ActionResult .Net Core

Delete ActionResult .Net Core

提问人:bootsy 提问时间:7/1/2019 更新时间:7/1/2019 访问量:2565

问:

我收到错误“运算符”==“不能应用于此行类型为”string“和”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));
     }
  }

我的预期结果是,当我单击“查看”页面上的“删除”链接时,它会要求确认删除,然后在单击“是”或“否”后,它会执行 DeleteConfirmed 操作。我需要什么来修复这个错误?

asp.net-core .net-core

评论

1赞 Panagiotis Kanavos 7/1/2019
该错误很明显,并指出代码中存在问题。是字符串还是整数?不能两者兼而有之。使用正确的类型。id
0赞 Chris Pratt 7/1/2019
的默认主键列类型是字符串,而不是 int。如果您愿意,可以将其设置为 int,但这不是默认设置。此外,FWIW,您应该始终使用 管理用户,而不是直接使用上下文。在创建用户等时,它会在后台执行许多应用代码不应关注的操作(例如规范化用户名和电子邮件)。您可以通过上下文选择用户,但标准 CRUD 应通过管理器进行。IdentityUserUserManager<TUser>

答:

2赞 TanvirArjel 7/1/2019 #1

更改为和更改为如下:Delete(int? id)Delete(string id)DeleteConfirmed(int id)DeleteConfirmed(string 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));
}