views:

369

answers:

5

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

I need it to render dynamic data so.... this means that every controller action needs to return viewData which contains this data.. as well as the action specific data

How would you guys implement this? The constructor of a base controller perhaps?

+1  A: 

You could pass data to a master page and show a footer there:

http://stackoverflow.com/questions/78548/passing-data-to-master-page-in-asp-net-mvc

bbmud
A: 

If the data isn't complex I use the ViewData dictionary to store this information. If it is, I put the footer data in a base class for all models, then move the footer into a ContentPlaceholder. This is a little more annoying as you have to put it on every View page.

Will
+5  A: 

You're on the right track with the base controller idea, but I would override OnActionExecuted and generate the data there. Check to see if the result is going to be a ViewResult before generating the common data. There's no need to generate the data if the result is a redirect or data going back via AJAX.

You might also consider creating a view-only model for the common data (if the data is extensive) and putting it in ViewData as a whole. Then you can create a strongly-typed partial view that takes the model and use the model's properties more easily in the view. Rendering this partial view from the master page would make it easy to both incorporate the data on every page and use it in a strongly-typed way.

If the footer data or formatting isn't complicated, then just putting the mark up in the master page is probably better.

tvanfosson
+1  A: 

You can create an ActionFilter that writes the data into the ViewData like so :

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

Then decorate your controllers and/or actions that's gonna need that data :

[CustomActionFilter]
public class HomeController : Controller {
   ...
}
çağdaş
+1  A: 

What 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 might argue that this muddies the waters of View and Controller separation, and I probably wouldn't do this in a ViewPage, but I see it as a decent alternative in a MasterViewPage.

Flame On ;)

GuyIncognito