I have created a web app from the .NET Web Application template. This app should display heroes and their superpowers.
This is my controller method:
public IActionResult GetHero(int id)
{
if (!ModelState.IsValid)
{
return HttpBadRequest(ModelState);
}
Hero hero = _context.Hero.Include(m => m.SuperPowers).Single(m => m.Id == id);
if (hero == null)
{
return HttpNotFound();
}
return Json(hero);
}
And this is my model:
public class Hero
{
public int Id { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public virtual ICollection<SuperPower> SuperPowers { get; set; }
}
If I use
return Json(hero);
like in the controller code above I get a "Bad Gateway" error but if I use
return View(hero);
I can display the hero and the related superpowers in a view that I created.
What am I doing wrong?