I am making a system for the client, and he did not use fullcalendar, which would be practical. With that, I created in a table with the fields AgendaId (int), and DateAgendar (DateTime) years forward (some 20000 lines).
With that I set up the following ViewModel:
public class AgendaAgendamentoVM
{
public int Id { get; set; }
public int? AgendamentoId { get; set; }
public DateTime Date { get; set; }
public int? ClienteId { get; set; }
public string ClienteNome { get; set; }
}
And I have a second table called Reserva with the fields:
public class Reserva
{
public int ReservaId { get; set; }
public int AgendaId { get; set; }
[Required]
public int ClienteId { get; set; }
public string Servico { get; set; }
public virtual Cliente Cliente { get; set; }
}
and Clients:
public class Cliente
{
public int ClienteId { get; set; }
[Column(TypeName = "varchar(50)")]
public string Nome { get; set; }
[Column(TypeName = "varchar(50)")]
public string Telefone { get; set; }
[Column(TypeName = "varchar(50)")]
public string Endereco { get; set; }
[Column(TypeName = "varchar(50)")]
public string Email { get; set; }
public DateTime DataCadastro { get; set; }
}
The ViewModel code below returns every day from 6 am until 10 pm:
IList<AgendaAgendamentoVM> _listaAgendaVM = new List<AgendaAgendamentoVM>();
if (!String.IsNullOrEmpty(mes))
{
var data = DateTime.Parse(mes);
ViewBag.Data = data;
ViewBag.Ontem = data.AddDays(-1);
ViewBag.Amanha = data.AddDays(1);
var dias = await _context.Agenda
.Where(x => x.DataAgenda >= data.AddHours(6))
.Where(x => x.DataAgenda <= data.AddHours(22))
.ToListAsync();
foreach (var item in dias)
{
AgendaAgendamentoVM li = new AgendaAgendamentoVM();
li.Id = item.AgendaId;
li.Date = DateTime.Parse(item.DataAgenda.ToString());
_listaAgendaVM.Add(li);
}
ViewData["ListaAgenda"] = _listaAgendaVM;
return View();
The View:
@{
ViewBag.Title = "Agenda";
}
<form>
<input type="date" name="mes" required /> <input type="submit" value="Enviar" />
</form>
<div class="row m-1 p-2 bg-success rounded">
<div class="col-4">
<a class="text-white" href="/Agenda/Index?mes=@String.Format("{0:d}", ViewBag.Ontem)"><</a>
</div>
<div class="col-4 text-center text-white font-weight-bolder">
@String.Format("{0:d}", ViewBag.Data)
</div>
<div class="col-4 text-right">
<a class="text-white" href="/Agenda/Index?mes=@String.Format("{0:d}", ViewBag.Amanha)">></a>
</div></div>
<ul class="list-group m-1">
@foreach (var item in ViewData["ListaAgenda"] as
List<Camarim.Core.ViewModels.AgendaAgendamentoVM>)
{
<li class="list-group-item"><a href="#?agendaid=@item.Id">@String.Format("{0:HH:mm}",
item.Date) - @item.ClienteNome</a></li>
}
Only with the Agenda table, I can display in a view a "navigable" calendar as shown in the image:
Still in the Reservation table, I have a ClienteId field, where I return a specific customer. In the List I make of the ViewModel, I use a foreach calling the Reservation and Customers table, as shown in the photo:
The problem with this is, when doing this, the view stops showing the entire list from 6 am to 10 pm, it only shows the fields where clients are scheduled. My goal was to leave it at least this way:
How can I do to achieve this goal: and / OR another: Is there any other way to do this without using the database for popular dates?
It seems that you want to display the ClientNome
.
Firstly,from your expected result image,it should be one-to-many relationship between Agenda
and Reserva
.Then you could get the multiple ClientNome
to display it in your razor view.Change your model like below:
public class Agenda
{
public int AgendaId { get; set; }
public DateTime DataAgenda { get; set; }
public List<Reserva> Reserva { get; set; }
}
public class Reserva
{
public int ReservaId { get; set; }
public int AgendaId { get; set; }
[Required]
public int ClienteId { get; set; }
public string Servico { get; set; }
public virtual Cliente Cliente { get; set; }
}
public class Cliente
{
public int ClienteId { get; set; }
[Column(TypeName = "varchar(50)")]
public string Nome { get; set; }
[Column(TypeName = "varchar(50)")]
public string Telefone { get; set; }
[Column(TypeName = "varchar(50)")]
public string Endereco { get; set; }
[Column(TypeName = "varchar(50)")]
public string Email { get; set; }
public DateTime DataCadastro { get; set; }
}
public class AgendaAgendamentoVM
{
public int Id { get; set; }
public int? AgendamentoId { get; set; }
public DateTime Date { get; set; }
public int? ClienteId { get; set; }
public List<string> ClienteNome { get; set; }
}
View:
<form>
<input type="date" name="mes" required /> <input type="submit" value="Enviar" />
</form>
<div class="row m-1 p-2 bg-success rounded">
<div class="col-4">
<a class="text-white" href="/Agenda/Index?mes=@String.Format("{0:d}", ViewBag.Ontem)"><</a>
</div>
<div class="col-4 text-center text-white font-weight-bolder">
@String.Format("{0:d}", ViewBag.Data)
</div>
<div class="col-4 text-right">
<a class="text-white" href="/Agenda/Index?mes=@String.Format("{0:d}", ViewBag.Amanha)">></a>
</div>
</div>
<ul class="list-group m-1">
@foreach (var item in ViewData["ListaAgenda"] as List<AgendaAgendamentoVM>)
{
<li class="list-group-item">
<a href="#?agendaid=@item.Id">
@String.Format("{0:HH:mm}",
item.Date) - @foreach (var i in item.ClienteNome)
{
@i @string.Format(" ") @*change this*@
}
</a>
</li>
}
</ul>
Controller:
public async Task<IActionResult> Index(string mes)
{
IList<AgendaAgendamentoVM> _listaAgendaVM = new List<AgendaAgendamentoVM>();
if (!String.IsNullOrEmpty(mes))
{
var data = DateTime.Parse(mes);
ViewBag.Data = data;
ViewBag.Ontem = data.AddDays(-1);
ViewBag.Amanha = data.AddDays(1);
var dias = await _context.Agenda
.Where(x => x.DataAgenda >= data.AddHours(6))
.Where(x => x.DataAgenda <= data.AddHours(22))
.ToListAsync();
foreach (var item in dias)
{
var reservas = (from p in _context.Reservas
join c in _context.Clientes on p.ClienteId equals c.ClienteId
where p.AgendaId == item.AgendaId
select c).ToList();
AgendaAgendamentoVM li = new AgendaAgendamentoVM();
li.Id = item.AgendaId;
li.Date = DateTime.Parse(item.DataAgenda.ToString());
li.ClienteNome = reservas.Select(a => a.Nome).ToList();
_listaAgendaVM.Add(li);
}
ViewData["ListaAgenda"] = _listaAgendaVM;
return View();
}
return View();
}