【发布时间】:2021-05-08 04:51:32
【问题描述】:
我正在尝试学习来自在线资源(Udemy 课程)的教程,但我尝试搜索甚至多次查看我收到错误的原因的代码中似乎存在错误。
首先,我是 c#/.net/razor 的新手,这是课堂上正在教授的技术堆栈。讲师在他的代码中没有收到错误,所以我迷路了(他似乎也没有回应任何人)。
所以我有一个名为 BookControllers 的控制器,它由以下代码组成
enter code here
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using BookListRazor.Model;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
namespace BookListRazor.Controllers
{
[Route("api/Book")]
[ApiController]
public class BookController : Controller
{
private readonly ApplicationDbContext _db;
public BookController(ApplicationDbContext db)
{
_db = db;
}
[HttpGet]
public async Task<IActionResult> GetAll()
{
return Json(new { data = await _db.Book.ToListAsync() });
}
[HttpDelete]
public async Task<IActionResult> Delete(int id)
{
var bookFromDb = _db.Book.FirstOrDefaultAsync(u => u.Id == id);
if (bookFromDb == null)
{
return Json(new { success = false, message = "Error while Deleting" });
}
_db.Book.Remove(bookFromDb); //This is where the error is being produced at in the code from the parameter that is being provide.
await _db.SaveChangesAsync();
return Json(new { success = true, message = "Delete successful" });
}
}
}
【问题讨论】:
标签: c# asp.net .net asp.net-mvc razor