I have 2 foreachs
, to do a date search in ViewBags
, where I return both in a select.
But for each item in the first foreach, it makes a full loop in the second, thus doubling the values, by each item in the first.
Ex: If I have 3 items in the first foreach
and 2 items in the second, my final result will be 6 items in my select
, and it repeats the data after the loop of the first one.
My problem is: How do you not repeat this data?
My Foreahcs in the view:
<div class="col-md-9">
<select class="form-control" style="width:250px" id="selectPeriodo" name="sAquisitivo">
@foreach (var date in ViewBag.Ferias)
{
foreach (var fim in ViewBag.FimFerias)
{
<option>
@Convert.ToDateTime(date).ToShortDateString() à @Convert.ToDateTime(fim).ToShortDateString()
</option>
}
}
</select>
</div>
My ViewBags:
ViewBag.Ferias = funcionarioFeriasRepository.Lista.Where(r => r.CdMatricula == matricula && r.SqContrato == contrato && r.DtInicioConcessao == null)
.Select(x => x.DtInicioPeriodo).ToList();
ViewBag.FimFerias = funcionarioFeriasRepository.Lista.Where(r => r.CdMatricula == matricula && r.SqContrato == contrato && r.DtInicioConcessao == null)
.Select(x => x.DtFimPeriodo).ToList();
Entity:
[DataType(DataType.Date)]
[DisplayFormat(DataFormatString = "{0:dd/MM/yyyy}", ApplyFormatInEditMode = true)]
public DateTime? dtInicioFerias { get; set; }
[DataType(DataType.Date)]
[DisplayFormat(DataFormatString = "{0:dd/MM/yyyy}", ApplyFormatInEditMode = true)]
public DateTime? dtFimFerias { get; set; }
public string sAquisitivo { get; set; }
Result:
Remembering that I'm working with Foreachs because it was the only option I could find to format the dates for the dd / MM / yyyy format. Because they in a DropDownList were not formatted, even using toString () and adding annotations to the model.