当前,我有2个控制器和两个不同的表,当我使用PostMan时,它会插入到bar表中,但当我重定向它时,它不会插入到Users表中,而是要求更多的值,以便可以将该对象放置到Bars表中再次。 '
namespace UrLocal.Controllers
{
[ApiController]
[Route("api/[controller]")]
public class BarsController : ControllerBase
{
private readonly UrLocalContext _db;
public BarsController(UrLocalContext db)
{
_db = db;
}
// Action Methods
// GET: api/values
[HttpGet]
public IActionResult GetBars()
{
return Ok(_db.bars.ToList());
}
// POST api/values
[HttpPost]
public async Task<IActionResult> AddBar([FromBody] Bars objBars)
{
if (!ModelState.IsValid)
{
return new JsonResult("Error While Creating New Bar");
}
_db.Add(objBars);
await _db.SaveChangesAsync();
return new JsonResult("Bar inserted successfully");
}
// PUT api/values/5
[HttpPut("{id}")]
public async Task<IActionResult> UpdateBar([FromRoute] int id, [FromBody] Bars objBars)
{
if (objBars ==null || id != objBars.barId)
{
return new JsonResult("This Bar cannot be updated");
}
else
{
_db.bars.Update(objBars);
await _db.SaveChangesAsync();
return new JsonResult("Bar has been updated");
}
}
// DELETE api/values/5
[HttpDelete("{id}")]
public async Task<IActionResult> DeleteBar([FromRoute]int id)
{
var findBar = await _db.bars.FindAsync(id);
if (findBar == null) return NotFound();
else
{
_db.bars.Remove(findBar);
await _db.SaveChangesAsync();
return new JsonResult("Bar Was Removed Successfully");
}
}
}
}
namespace UrLocal.Controllers
{
[ApiController]
[Route("api/[controller]")]
public class UsersController : ControllerBase
{
private readonly UrLocalContext _db;
public UsersController(UrLocalContext db)
{
_db = db;
}
// Action Methods
// GET: api/values
[HttpGet]
public IActionResult GetUsers()
{
return Ok(_db.users.ToList());
}
// POST api/values
[HttpPost]
public async Task<IActionResult> AddUsers([FromBody] Users objUsers)
{
if (!ModelState.IsValid)
{
return new JsonResult("Error While Creating New User");
}
_db.users.Add(objUsers);
await _db.SaveChangesAsync();
return new JsonResult("User inserted successfully");
}
// PUT api/values/5
[HttpPut("{id}")]
public async Task<IActionResult> UpdateUser([FromRoute] int id, [FromBody] Users objUser)
{
if (objUser == null || id != objUser.userId)
{
return new JsonResult("This User cannot be updated");
}
else
{
_db.users.Update(objUser);
await _db.SaveChangesAsync();
return new JsonResult("User has been updated");
}
}
// DELETE api/values/5
[HttpDelete("{id}")]
public async Task<IActionResult> DeleteUser([FromRoute]int id)
{
var findUser = await _db.users.FindAsync(id);
if (findUser == null) return NotFound();
else
{
_db.users.Remove(findUser);
await _db.SaveChangesAsync();
return new JsonResult("User Was Removed Successfully");
}
}
}
}
对于我检查的问题可能是什么,我感到非常困惑,因为我正在将其输入到正确的表中,并且一切都正确,但是似乎所有这些都继续想要将值输入到bars表中。
我也知道连接是可以的,因为我可以“获取”它会返回一个空的JSON文件,但是每当我尝试发布时,它都不起作用