Abdul Ahmad
Abdul Ahmad

Reputation: 10021

asp.net mvc controller calculating incorrect values

this is seriously bugging me right now. I have the 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;
        for (int i = 0; i < 4; i++)
        {
            if ((compyear = Employee.CompensationYear.Find(m => m.Year == --latestYearDisplayedMinusThree)) != null)
            {
                fourYearsList.Add(compyear);
            }
            else
            {
                break;
            }
        }
        fourYearsList.Reverse();
    }
    else if (direction == "left")
    {

        for (int i = 0; i < 4; i++)
        {
            if ((compyear = Employee.CompensationYear.Find(m => m.Year == ++latestYearCurrentlyDisplayed)) != null)
            {
                fourYearsList.Add(compyear);
            }
            else
            {
                break;
            }
        }
    }

    return PartialView(fourYearsList);
}

there are two problems, I'm calling this method from an Html.Ajax helper and the "latestYearCurrentlyDisplayed" is returning 2020 (which I have in the database for employee.compensationYear). By the way I have data from the year 2014 to 2020 for this model in the database.

so anyway, first problem, when 2020 is returned, and I get the direction "right", I'm creating a new int variable that gets 2020-3 which should equal 2017. Then in the loop, in the if, I'm assigning compeer to the employee's compensationYear where the year is equal to "--latestYearDisplayedMinusThree". This should subtract 1 from 2017 first, then assign it, but its not, for some reason, in my first item in the list I'm getting 2015 not 2016.

the other problem is, when I'm getting "left" and 2015 as my latest displayed year, its going into the else if (direction == "left") which is great, but then its returning null for where I assign compyear, even though I have ++2015 (again, I have 2014 to 2020).

have I done something wrong in the code here??

Upvotes: 0

Views: 65

Answers (1)

Brent Mannering
Brent Mannering

Reputation: 2316

I believe the problem is how the unary (++x and --x) operators work inside the loops, and perhaps not doing what you expect them to do.

However if I understand what you want correctly, then this should simplify things a 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 and latestYearCurrentlyDisplayed + 1 may need adjusting depending on what you want to display, and also the ordering depending how you want the list displayed.

Upvotes: 1

Related Questions