Reputation: 159
I'm making a simple website and in the top section of the layout page in MVC I want to have quotes outputted everytime the page refreshes. I'm going to pull data from database and want to output it in the _Layout. But I haven't got the slightest idea of how to do it. And trying to google it didn't work.
ViewBag and ViewData wouldn't work for me, because I will have to set them up in every controller action I have..
So can you suggest something?
Upvotes: 0
Views: 301
Reputation: 874
I don't know if i understood realy what you are trying to do. But if you want to ouput something in your layout view., you can try to use child Action Method and you wil not have to call a method in each controller. here is an example Child Action Methods in ASP.NET MVC
[ChildActionOnly]
public ActionResult GetNews(string category)
{
var newsProvider = new NewsProvider();
var news = newsProvider.GetNews(category);
return PartialView(news);
}
Above child action method can be invoked inside any view in the application using Html.RenderAction() or Html.Action() method.
@Html.Action("GetNews", "Home", new { category = "Sports"})
(or)
@{ Html.RenderAction("GetNews", "Home", new { category = "finance"}); }
Putting this code inside the _layout view make it available to all page that use that layout view.
Added on comment: You need a view in the Home folder (the controller folder) that have the same name with your method name.
Upvotes: 1