当前位置: 动力学知识库 > 问答 > 编程问答 >

asp.net mvc - Foreach loop binding all data in first iteration

问题描述:

Guys in my ASP MVC 4 web application, I am trying to bind education details to a model Candidate model. Now what happens is that, on the first iteration of foreach loop, it binds the same data to all the candidates. I have been stuck on this for more than 4 hours now and can't seem to figure it out...

In my controller :

 if (dsResult.Tables[18].Rows.Count > 0)

{

foreach (DataRow drRequiredEducation in dsResult.Tables[18].Rows)

{

var jobDescriptionCode = Convert.ToInt32(drRequiredEducation["JobDescriptionCode"].ToString());

var education = new Education();

education.EducationCode = Convert.ToInt32(drRequiredEducation["EducationCode"].ToString());

education.EducationName = drRequiredEducation["EducationName"].ToString();

var candidates = candidatePreOfferViewModel.Candidates.Where(x => x.JobRequisition.JobDescriptionCode == jobDescriptionCode);

foreach (var candidate in candidates)

{

candidate.JobRequisition.JobDescription.JobDescriptionCode = jobDescriptionCode;

candidate.JobRequisition.JobDescriptionCode = jobDescriptionCode;

candidate.JobRequisition.JobDescription.Educations.Add(education);

}}}

In my CandidatePreOfferViewModel model :

public class CandidatePreOfferViewModel

{

public CandidatePreOfferViewModel()

{

this.Candidates = new List<Candidate>();

this.DepartmentCandidates = new List<DepartmentCandidate>();

}

public int SelectedDepartmentCode { get; set; }

public string SelectedCandidateStatusCode { get; set; }

public List<Candidate> Candidates { get; set; }

public List<DepartmentCandidate> DepartmentCandidates { get; set; }

}

Candidate Model :

public class Candidate

{

public Candidate()

{

this.Educations = new List<Education>();

this.Experiences = new List<Experience>();

this.JobRequisition = new JobRequisition();

this.Performances = new List<CandidatePerformance>();

this.RequiredEducationForRequisition = new List<Education>();

}

.....xyz....

public List<Education> Educations { get; set; }

public List<Experience> Experiences { get; set; }

public JobRequisition JobRequisition { get; set; }

public List<CandidatePerformance> Performances { get; set; }

public List<Education> RequiredEducationForRequisition { get; set; }

}

In my view :

<span>Required:

@foreach (var education in candidate.JobRequisition.JobDescription.Educations)

{

<div>[email protected]</div>

}

</span>

Now the problem is, on the first iteration it sets the education for all 9 candidates and then for the second iteration it again adds the same education and so on, as a result I am getting repeated data like in this picture (education is repeated 9 times because there were total of 9 candidates, also there are only 3 entries for education in db) :

I am going nuts trying to figure out the problem, so your help will really make my day.

网友答案:

In your second for loop , check if that perticular education is already added for that candidate

foreach (var candidate in candidates) {

candidate.JobRequisition.JobDescription.JobDescriptionCode = jobDescriptionCode; candidate.JobRequisition.JobDescriptionCode = jobDescriptionCode;

      //check if this education is already added.
    var alreadyAdded = candidate.JobRequisition.JobDescription.Educations.Where(e=>e.EducationName==education.EducationName).FirstOrDefault();

    if(alreadyAdded!=null) 
    {
      candidate.JobRequisition.JobDescription.Educations.Add(education);
    }
}}}

This .Where is linq , you may need to add using System.Linq; else you can loop thru candidates Educations and see if education.EducationName is present or not;

分享给朋友:
您可能感兴趣的文章:
随机阅读: