0
I am not able to remove an ID when I click... Error message appears: System.Invalidoperationexception: ː Sequence contains no Elements'
Follow my code below:
Institucaocontroller:
using Capitulo01.Models;
using Microsoft.AspNetCore.Mvc;
using System.Collections.Generic;
using System.Linq;
namespace Capitulo01.Controllers
{
public class InstituicaoController : Controller
{
private static IList<Instituicao> instituicoes =
new List<Instituicao>()
{
new Instituicao() {
InstituicaoID = 1,
Nome = "UniParaná",
Endereco = "Paraná"
},
new Instituicao() {
InstituicaoID = 2,
Nome = "UniSanta",
Endereco = "Santa Catarina"
},
new Instituicao() {
InstituicaoID = 3,
Nome = "UniSãoPaulo",
Endereco = "São Paulo"
},
new Instituicao() {
InstituicaoID = 4,
Nome = "UniSulgrandense",
Endereco = "Rio Grande do Sul"
},
new Instituicao() {
InstituicaoID = 5,
Nome = "UniCarioca",
Endereco = "Rio de Janeiro"
}
};
public IActionResult Index()
{
return View(instituicoes);
}
// GET: Create
public ActionResult Create()
{
return View();
}
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create(Instituicao instituicao)
{
instituicoes.Add(instituicao);
instituicao.InstituicaoID =
instituicoes.Select(m => m.InstituicaoID).Max() + 1;
return RedirectToAction("Index");
}
public ActionResult Edit(long id)
{
return View(instituicoes.Where(
m => m.InstituicaoID == id).First());
}
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Edit(Instituicao instituicao)
{
instituicoes.Remove(instituicoes.Where(
c => c.InstituicaoID == instituicao.InstituicaoID)
.First());
instituicoes.Add(instituicao);
return RedirectToAction("Index");
}
public ActionResult Details(long id)
{
return View(instituicoes.Where(
m => m.InstituicaoID == id).First());
}
// GET: Delete
public ActionResult Delete(long id)
{
return View(instituicoes.Where(
m => m.InstituicaoID == id).First());
}
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Delete(Instituicao instituicao)
{
instituicoes.Remove(instituicoes.Where(
c => c.InstituicaoID == instituicao.InstituicaoID)
.First());
return RedirectToAction("Index");
}
}
}
You even confirm that in Delete the object with the ID is coming?
– Barbetta
How is your method
Remove
?– Marconi