3
我正在處理一段代碼,該代碼顯示每個網頁上的隨機贊助商圖像。我認爲調用我的函數的最佳位置在Views/Shared/_Layout.cshtml頁面中,因爲這是每個頁面加載的頁面。路徑'/'的控制器未找到或未實現IController
我在我的域模型的服務類中編寫了函數,並在我的homecontroller中編寫了ChildActionOnly
函數,在Views/Home/Randomsponsor.cshtml中的簡單視圖中返回值,並在共享佈局中調用該函數一個Html.action。
一切都建立正常,但運行時,我得到了一個錯誤:
{"The controller for path '/' was not found or does not implement IController."}
有誰知道如何解決這個問題呢?
法域項目:
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;
}
在HomeController的:在查看/家庭
[HttpGet]
[ChildActionOnly]
public ActionResult RandomSponsor()
{
var model = service.advertsForCountry();
return PartialView("RandomSponsor", model);
}
簡單視圖/:
@{
ViewBag.Title = "RandomSponsor";
}
@Html.Action("RandomSponsor")
而且在查看我的函數調用/共享/ _Layout.cshtml包含導航欄等等:
@Html.Action("RandomSponsor", "HomeController")
問候。
嘗試使用'@ Html.Action(「RandomSponsor」,「Home」)'而不是'@ Html.Action(「RandomSponsor」,「HomeController」)' – wgraham
@wgraham。那樣做了,謝謝! – Gijs