我收到错误信息'运算符'=='无法应用于类型为'string'和'int?'的操作数在这条线上
public static func request(_ url: URLConvertible, method: Alamofire.HTTPMethod = .get, parameters: Parameters? = nil, encoding: ParameterEncoding = URLEncoding.default, headers: Alamofire.HTTPHeaders? = nil, interceptor: RequestInterceptor? = nil) -> Alamofire.DataRequest
// or
public static func request<Parameters>(_ url: URLConvertible, method: Alamofire.HTTPMethod = .get, parameters: Parameters? = nil, encoder: ParameterEncoder = URLEncodedFormParameterEncoder.default, headers: Alamofire.HTTPHeaders? = nil, interceptor: RequestInterceptor? = nil) -> Alamofire.DataRequest where Parameters : Encodable
到目前为止,除此方法外,我没有尝试任何其他方法。
.FirstOrDefaultAsync(m => m.Id == id);
我的预期结果是,当我单击“视图”页面上的“删除”链接要求其确认删除,然后单击“是”或“否”时,它将执行DeleteConfirmed Action。我该如何解决此错误?
答案 0 :(得分:2)
将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));
}