0
I’m trying to finish a project only that is showing error on the foreach part, and in VSCODE does not report any problem. I’m learning C# now and I can’t figure it out on my own.
this is homecontroller:
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using Projeto_PetVet.Models;
namespace Projeto_PetVet.Controllers
{
public class HomeController : Controller
{
private readonly ILogger<HomeController> _logger;
public HomeController(ILogger<HomeController> logger)
{
_logger = logger;
}
public IActionResult Index()
{
return View();
}
public IActionResult Servicos()
{
return View();
}
public IActionResult PreAgendamento()
{
return View();
}
[HttpPost]
public IActionResult PreAgendamento(DadosConsulta consulta)
{
Consulta.addDados(consulta);
return View("olhaDados");
}
public IActionResult Sucesso()
{
return View();
}
public IActionResult olhaDados() {
List<DadosConsulta> consulta = Consulta.olhaDados();
return View(consulta);
}
[ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
public IActionResult Error()
{
return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier });
}
}
}
These are my Models: Query.Cs:
using System.Collections.Generic;
namespace Projeto_PetVet.Models
{
public class Consulta
{
static List<DadosConsulta> infos = new List<DadosConsulta>();
public static void addDados(DadosConsulta consulta) {
infos.Add(consulta);
}
public static List<DadosConsulta> olhaDados() {
return infos;
}
}
}
Dadosconsulta.Cs
namespace Projeto_PetVet.Models
{
public class DadosConsulta
{
public string Nome {get; set;}
public string Animal {get; set;}
public string Data {get; set;}
public string Telefone {get; set;}
public string Horario {get; set;}
public string Necessidade {get; set;}
}
}
And this is my View, where the bug is showing up in the foreach:
@model List<DadosConsulta>
@{
ViewData["Title"] = "Pré agendamento";
}
<h1>Confirme suas informações</h1>
<p>Confirme seu pré agendamento.</p>
<p>Todos os dados estão corretos?</p>
<table class="table table-bordered">
<tr>
<td>Nome</td>
<td>Telefone</td>
<td>Data</td>
<td>Horário</td>
<td>Animal</td>
<td>Necessidade</td>
</tr>
@{
Consulta infos = new Consulta();
if (Model != null) { foreach (Dadosconsulta consulta in Model) { @query. Name @query. Telephone @query. Date @query. Time @query. Animal @query. Need } } }
<a asp-action="Sucesso">Tudo certo!</a>
<a asp-action="PreAgendamento">Corrigir dados</a>
You have to fill the values of the objects before entering the foreach. Right and fill the values in the backend searching in the database
– Flavio C Web Developer Angular
I have not yet left for the database :( But I will fix the code again, thank you for the tips
– Vitória Oliveira