提问人:bootsy 提问时间:7/1/2019 更新时间:7/1/2019 访问量:2565
删除 ActionResult .Net Core
Delete ActionResult .Net Core
问:
我收到错误“运算符”==“不能应用于此行类型为”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 操作。我需要什么来修复这个错误?
答:
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));
}
评论
id
IdentityUser
UserManager<TUser>