Gij*_*ijs 3 c# asp.net-mvc render razor
我正在编写一段代码,在每个网页上显示随机赞助商图片.我认为调用我的函数的最佳位置是在Views/Shared/_Layout.cshtml页面中,因为那是在每个页面加载的那个.
我在我的域模型的服务类中编写了ChildActionOnly函数,并在我的homecontroller中编写了一个函数,在Views/Home/Randomsponsor.cshtml的简单视图中返回值,并在Html.action的共享布局中调用该函数.
一切都很好,但运行时我得到下一个错误:
{"The controller for path '/' was not found or does not implement IController."}
Run Code Online (Sandbox Code Playgroud)
有谁知道如何解决这个问题?
域项目中的方法:
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;
}
Run Code Online (Sandbox Code Playgroud)
在HomeController中:
[HttpGet]
[ChildActionOnly]
public ActionResult RandomSponsor()
{
var model = service.advertsForCountry();
return PartialView("RandomSponsor", model);
}
Run Code Online (Sandbox Code Playgroud)
在Views/Home /中的简单视图:
@{
ViewBag.Title = "RandomSponsor";
}
@Html.Action("RandomSponsor")
Run Code Online (Sandbox Code Playgroud)
我在View/Shared/_Layout.cshtml中的函数调用whitch包含导航栏等:
@Html.Action("RandomSponsor", "HomeController")
Run Code Online (Sandbox Code Playgroud)
问候.
wgr*_*ham 10
(转换自评论)
您无需在中指定Controller的完整类名Html.Action.尝试使用 @Html.Action("RandomSponsor", "Home")而不是@Html.Action("RandomSponsor", "HomeController").
| 归档时间: |
|
| 查看次数: |
8741 次 |
| 最近记录: |