Matchbox2093
Matchbox2093

Reputation: 986

How to get variables from another controller

I have two controllers: job and home

now job counts the number of stopped jobs and at which location like below

public void Stopped()
    {

        int HBAStop = db.Jobs.Where(x => x.Status == "Stopped" && x.LocationID == 1).Count();
        int CRStop =  db.Jobs.Where(x => x.Status == "Stopped" && x.LocationID == 2).Count();
        int MAStop =  db.Jobs.Where(x => x.Status == "Stopped" && x.LocationID == 3).Count();
        int QCStop =  db.Jobs.Where(x => x.Status == "Stopped" && x.LocationID == 4).Count();
        int LTStop =  db.Jobs.Where(x => x.Status == "Stopped" && x.LocationID == 5).Count();
        int PTStop =  db.Jobs.Where(x => x.Status == "Stopped" && x.LocationID == 6).Count();
        int SPStop =  db.Jobs.Where(x => x.Status == "Stopped" && x.LocationID == 7).Count();

    }

i then want in the home controller to initialise the job controller and grab the individual variables from the stopped() method, like below

 ViewBag.HBAStopped = j.Stopped();

how do i add the HBAStop variable to the viewbag? i can get around all this by making them into return methods each but this equals to about 30 methods and seems a bit overkill and untidy. I tried various ways with no success, with the return methods being the only option at the moment

i can provide further information if needed, thank you

Upvotes: 0

Views: 345

Answers (2)

Ali pishkari
Ali pishkari

Reputation: 542

//1. create struct:

  struct JobStops
{
    int HBAStop;
    int CRStop;
    int MAStop;
    int QCStop;
    int LTStop;
    int PTStop;
    int SPStop;

 }

//2- update your method like this:

public JobStops Stopped()
    {
        JobStops _jobstops ;
        _jobstops. HBAStop = db.Jobs.Where(x => x.Status == "Stopped" && x.LocationID == 1).Count();
        _jobstops. CRStop =  db.Jobs.Where(x => x.Status == "Stopped" && x.LocationID == 2).Count();
        _jobstops. MAStop =  db.Jobs.Where(x => x.Status == "Stopped" && x.LocationID == 3).Count();
        _jobstops. QCStop =  db.Jobs.Where(x => x.Status == "Stopped" && x.LocationID == 4).Count();
        _jobstops. LTStop =  db.Jobs.Where(x => x.Status == "Stopped" && x.LocationID == 5).Count();
        _jobstops. PTStop =  db.Jobs.Where(x => x.Status == "Stopped" && x.LocationID == 6).Count();
        _jobstops. SPStop =  db.Jobs.Where(x => x.Status == "Stopped" && x.LocationID == 7).Count();
    return _jobstops;

}

//3- ViewBag.HBAStopped = j.Stopped();

Upvotes: 0

DGibbs
DGibbs

Reputation: 14618

It seems like the only difference between each of the stopped counts is the LocationID. In this case you could just make a single method to handle them all:

public int GetStoppedCount(int locationId)
{
     return db.Jobs.Where(x => x.Status == "Stopped" 
            && x.LocationID == locationId).Count();
}

You could then do:

ViewBag.HBAStopped = GetStoppedCount(1);

etc...

Just a side note, I wouldn't rely too much on magic numbers e.g

db.Jobs.Where(x => x.Status == "Stopped" && x.LocationID == 1).Count();

Where the 1 is a bit ambigious. Prefer enums or static constants e.g:

public static class LocationConstants
{
     public const int HBAStop = 1;
     public const int CRStop = 2;

     // etc...
}

You can then do:

ViewBag.HBAStopped = GetStoppedCount(LocationConstants.HBAStop);

Which is much more readable.

Upvotes: 4

Related Questions