Foreach Duplicating Data

3

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.

    
asked by anonymous 02.02.2015 / 12:56

2 answers

3

As placed by @bigown, view should have as little processing as possible, ideally you should simply display the value in view and not convert data etc. ..

Consider the code below only if you can not deploy as the @bigown suggested. But focusing on your foreach problem, I believe you can try to mount these values without duplicating the dates like this:

...
@for(int i = 0; i < ViewBag.Ferias.Count(); i++)
{
   var l = 0;
   while(l < 1)
   {
      <option>
              @Convert.ToDateTime(ViewBag.Ferias[i]).ToShortDateString() à @Convert.ToDateTime(ViewBag.FimFerias[i]).ToShortDateString()
       </option>
      l++;
   }
}
...

Editing:

Removed parentheses from ViewBag.Ferias.Count to for .

Removed While , I had inserted it in the code above when I was thinking about the 2 foreach s, but it is not necessary.

...
@for(int i = 0; i < ViewBag.Ferias.Count; i++)
{
      <option>
              @Convert.ToDateTime(ViewBag.Ferias[i]).ToShortDateString() à @Convert.ToDateTime(ViewBag.FimFerias[i]).ToShortDateString()
       </option>
}
...
    
02.02.2015 / 14:25
4

I believe this is what you want. I see no reason to exist two foreach s. This solution was given because viewBag was created in the wrong way. Then it would look like this:

ViewBag.Ferias = funcionarioFeriasRepository.Lista.Where(r => r.CdMatricula == matricula && r.SqContrato == contrato && r.DtInicioConcessao == null)
        .Select(x => new { Inicio = x.DtInicioPeriodo, Fim = x.DtFimPeriodo}).ToList();

I did the conversion there but you can not do it if you need the data without conversion elsewhere. But in general it is not the right one, the view should have as little processing as possible. Then the view looks like this:

<div class="col-md-9">
    <select class="form-control" style="width:250px" id="selectPeriodo" name="sAquisitivo">
        @foreach (var date in ViewBag.Ferias) {
            <option>
                @Convert.ToDateTime(date.Inicio).ToShortDateString() à @Convert.ToDateTime(date.Fim).ToShortDateString()
            </option>
        }
    </select>
</div>

I have no way to test, maybe you have to do some adaptation to work but the general idea is this.

    
02.02.2015 / 13:14