视图模型列表中 DateTime 的良好实践和使用

Good practices and use of DateTime in List of ViewModels

我正在给客户做一个系统,他没有用fullcalendar,倒是实用。这样,我在 table 中创建了 AgendaId (int) 和 DateAgendar (DateTime) 字段(大约 20000 行)。

据此我设置了以下 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; }
}

我还有第二个 table 名为 Reserva 的字段:

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; }

    }

每天早上 6 点到晚上 10 点 returns 以下的 ViewModel 代码:

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();

视图:

@{
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>

}

只有议程 table,我才能在视图中显示“可导航”日历,如图所示:

仍然在预订 table 中,我有一个 ClienteId 字段,其中我 return 一个特定的客户。在我创建的 ViewModel 列表中,我使用 foreach 调用 Reservation 和 Customers table,如图所示:

这样做的问题是,执行此操作时,视图会从早上 6 点到晚上 10 点停止显示整个列表,它只显示安排客户的字段。我的目标是至少以这种方式离开它: Goal I want to achieve

我怎样才能实现这个目标:和/或另一个:有没有其他方法可以在不使用热门日期数据库的情况下做到这一点?

您似乎想要显示 ClientNome

首先,从你的预期结果图像来看,AgendaReserva之间应该是one-to-many关系。然后你可以得到多个ClientNome来显示它你的剃须刀 view.Change 你的型号如下:

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; }
}

查看:

    <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>

控制器:

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();
}

结果: