c# - asp.net mvc controller calculating incorrect values -
this bugging me right now. have following controller action method:
public partialviewresult scrollemployeecompyear(int employeeid, string direction, int latestyearcurrentlydisplayed) { list<employeecompensationyear> fouryearslist = new list<employeecompensationyear>(); employee employee = _db.employees.find(employeeid); employeecompensationyear compyear; if (direction == "right") { int latestyeardisplayedminusthree = latestyearcurrentlydisplayed - 3; (int = 0; < 4; i++) { if ((compyear = employee.compensationyear.find(m => m.year == --latestyeardisplayedminusthree)) != null) { fouryearslist.add(compyear); } else { break; } } fouryearslist.reverse(); } else if (direction == "left") { (int = 0; < 4; i++) { if ((compyear = employee.compensationyear.find(m => m.year == ++latestyearcurrentlydisplayed)) != null) { fouryearslist.add(compyear); } else { break; } } } return partialview(fouryearslist); }
there 2 problems, i'm calling method html.ajax helper , "latestyearcurrentlydisplayed" returning 2020 (which have in database employee.compensationyear). way have data year 2014 2020 model in database.
so anyway, first problem, when 2020 returned, , direction "right", i'm creating new int variable gets 2020-3 should equal 2017. in loop, in if, i'm assigning compeer employee's compensationyear year equal "--latestyeardisplayedminusthree". should subtract 1 2017 first, assign it, not, reason, in first item in list i'm getting 2015 not 2016.
the other problem is, when i'm getting "left" , 2015 latest displayed year, going else if (direction == "left")
great, returning null assign compyear, though have ++2015 (again, have 2014 2020).
have done wrong in code here??
i believe problem how unary (++x
, --x
) operators work inside loops, , perhaps not doing expect them do.
however if understand want correctly, should simplify things bit:
if (direction == "right") { fouryearslist = employee.compensationyear.where(m => m.year <= (latestyearcurrentlydisplayed - 4)).take(4).tolist(); } else if (direction == "left") { fouryearslist = employee.compensationyear.where(m => m.year >= (latestyearcurrentlydisplayed + 1)).take(4).tolist(); }
the start values latestyearcurrentlydisplayed - 4
, latestyearcurrentlydisplayed + 1
may need adjusting depending on want display, , ordering depending how want list displayed.
Comments
Post a Comment