asp.net mvc - Foreach loop binding all data in first iteration -
guys in asp mvc 4 web application, trying bind education
details model candidate
model. happens that, on first iteration of foreach loop, binds same data candidates. have been stuck on more 4 hours , can't seem figure out...
in 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 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 view :
<span>required: @foreach (var education in candidate.jobrequisition.jobdescription.educations) { <div>-@education.educationname</div> } </span>
now problem is, on first iteration sets education 9 candidates , second iteration again adds same education , on, result getting repeated data in picture (education repeated 9 times because there total of 9 candidates, there 3 entries education in db) :
i going nuts trying figure out problem, make day.
in second loop , check if perticular education added candidate
foreach (var candidate in candidates) {
candidate.jobrequisition.jobdescription.jobdescriptioncode = jobdescriptioncode; candidate.jobrequisition.jobdescriptioncode = jobdescriptioncode;
//check if education 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 linq , may need add using system.linq; else can loop thru candidates educations , see if education.educationname
present or not;
Comments
Post a Comment