Foreach循环在第一次迭代中绑定所有数据

时间:2014-06-27 19:56:47

标签: asp.net-mvc asp.net-mvc-4 foreach

我的ASP MVC 4 Web应用程序中的人,我正在尝试将education详细信息绑定到模型Candidate模型。现在发生的是,在foreach循环的第一次迭代中,它将相同的数据绑定到所有候选者。我现在已经坚持了4个多小时,似乎无法弄明白......

在我的控制器中:

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

在我的CandidatePreOfferViewModel模型中:

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

候选人模特:

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

在我看来:

<span>Required: 
    @foreach (var education in candidate.JobRequisition.JobDescription.Educations)
    {
        <div>-@education.EducationName</div>
    }
</span>

现在的问题是,在第一次迭代时,它为所有9个候选人设置了教育,然后在第二次迭代中再次添加相同的教育等等,结果我得到了重复数据,如图(教育)重复9次,因为共有9名候选人,数据库中只有3个教育条目:

enter image description here

我正在努力找出问题所在,所以你的帮助将真正成为我的一天。

1 个答案:

答案 0 :(得分:0)

在你的第二个for循环中,检查是否已为该候选人添加了特定教育

  

foreach(候选人候选人)       {

     

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

这个。有linq,你可能需要使用System.Linq添加;否则你可以循环通过候选人教育,看看是否存在education.EducationName;