i'm working on a mvc project and i want to display a sponsorimage on each page.
But i'm having difficulties with showing them into the shared layout page that is rendered on with each view.
I've created a function in my Domain service class where i search the student's school, because the school is linked to a country, not the student. When i got that countryId, i search through the countries of each Advert where the countryId is equal to the school's countryId. When that's the case, i look for the sponsor of that particular advert, put them into a SponsorList, select a random sponsor from that SponsorList and return the SponsorCompany (because i renamed each sponsorimage to the companyname).
Now i want to call that function into the shared layout, so every time the pages renders, a random sponsorimage is showed for that particular student. But i dont know how to call that function because shared layout has no controller class.
public String advertsForCountry()
{
String studentSchool = finder.getLoggedStudent().SchoolId;
int studentCountry = db.Schools.Find(studentSchool).CountryId;
List<Sponsor> sponsorsForStudent = new List<Sponsor>();
List<Advert> adverts = db.Adverts.ToList();
foreach(Advert adv in adverts)
{
foreach(Country cntry in adv.Countries)
{
if(cntry.CountryId == studentCountry)
{
sponsorsForStudent.Add(adv.Sponsor);
}
}
}
Random random = new Random();
int randomSP = random.Next(0, sponsorsForStudent.Count()-1);
string sponsorAdvert = sponsorsForStudent.ElementAt(randomSP).SponsorCompany;
return sponsorAdvert;
}
Sorry English is not my native language.