ASP.net mvc General Footer Data

So, I have a footer that will appear on every page of my web application.

I need this for rendering dynamic data, so .... this means that each action of the controller should return a viewData that contains this data, as well as specific data about the action

How do you guys implement this? Maybe the designer of the base controller?

+4
source share
5 answers

You're on the right track with the idea of ​​a basic controller, but I would override OnActionExecuted and generate the data there. Check if the result should be a ViewResult before generating shared data. There is no need to generate data if the result is a redirect or data returned via AJAX.

You can also consider creating a model only to view shared data (if the data is extensive) and put it in the ViewData as a whole. Then you can create a strongly typed partial view that takes the model and makes it easier to use the properties of the model in the view. Providing this partial view from the main page will facilitate both the inclusion of data on each page and their use in a strictly typified way.

If the footer data or formatting is not complicated, then it might be better to place a label on the main page.

+5
source

You can create an ActionFilter that writes data to the ViewData as follows:

 public class CustomActionFilter : ActionFilterAttribute { public override void OnResultExecuting(ResultExecutingContext filterContext) { filterContext.Controller.ViewData["WillNeedThis"] = "Foo"; } } 

Then decorate your controllers and / or actions that will be needed for this data:

 [CustomActionFilter] public class HomeController : Controller { ... } 
+2
source

You can transfer the data to the main page and show the footer:

Transferring data to the home page in ASP.NET MVC

+1
source

How about using the Html.RenderAction () method from the MVC Futures assembly ?

- Site.master -

  <div id="footer"> <% Html.RenderAction("SomeAction", "SomeController", new { someParam = ViewData["someValue"] }); %> </div> 

Some may argue that this hides the waters of separation of View and Controller, and I probably won’t do it in ViewPage, but I consider this a worthy alternative in MasterViewPage.

Flame On ;)

+1
source

If the data is not complex, I use the ViewData dictionary to store this information. If so, I placed the footer data in the base class for all models, and then moved the footer to the ContentPlaceholder. This is a little more annoying as you have to put it on every page of the view.

0
source

All Articles