I'm working on ASP.Net
with MVC 4
, and when doing a search / using filters I'll present a resulting list of data. To better handle this result I created a ViewModel
where I put information from several tables. Now to populate the list I'm doing the following:
I create a variable of data type of ViewModel
:
var resultFiltro = new FiltroSPlaneamentoViewModel();
And I create a list of the same data type:
List<FiltroSPlaneamentoViewModel> listaResultFiltro = new List<FiltroSPlaneamentoViewModel>();
Problem:
When you go through a foreach
to enter the data in the list, and adding the resultFiltro
variable in the same list, all other data in the list changes and becomes equal to resultFilto
.
Example of a foreach
I'm using:
//Pesquisar Técnico Responsável
var serv = db.Servicos.Where(s => s.NumTransportado == TecnicoResp).ToList();
foreach (var item in serv)
{
resultFiltro.idFiltro += 1;
resultFiltro.Serie = item.DadosComerciais.Serie;
resultFiltro.NumDoc = item.DadosComerciais.NumDoc;
resultFiltro.ServicoID = item.ServicosID;
resultFiltro.TecnicoResponsavel = item.NumTransportado;
listaResultFiltro.Add(resultFiltro);
}