views:

48

answers:

1

In my master page I have a menu that uses jquery ui accordion.

What is the best way to specify which item should be active?? the only thing I can think of is

$(document).ready(function() {
            $("#accordion").accordion({
                collapsible: true,
                autoHeight: false,
                active:<%=ViewData["active"] %>
            });          
        })

But it seems a little repetitive having to set ViewData["active"] everytime a View is called throughout my whole app... what do you think?

A: 

Have you considered putting the accordian in a PartialView? Then you place your code within the PartialView and away from the Master page.

Also you can have a base controller which can set the ViewData for you so now it's in one place.

Edit

It's the same as any other base class. Your controller inherits from Controller so your base class will need to do the same;

using System.Web.Mvc;

namespace MyAppControllers
{
    public class ControllerBase : Controller
    {
        protected override void Execute(System.Web.Routing.RequestContext requestContext)
        {
            ViewData["ApplicationName"] = CacheHelper.Get().Name;
            base.Execute(requestContext);
        }
    }
}

Now set your controller class to inherit from the ControllerBase class;

public class HomeController : ControllerBase

I've also implemented a CacheHelper but you can obviously use your own flavour of storing the current value.

griegs
*fly on the wall here, not trying to hijack question, NachoF ;)*griegs: do you mind elaborating on the emplementation of the base controller? :) Sounds interesting.Again, sorry don't mean to hijack question, I'm having the same pondering as you, NachoF.
bomortensen
@bomortensen The base controller is simply a class that inherits Controller. It allows you to place your own logic into the OnActionExecuting and OnActionExecuted (among other) methods to inject logic, log errors, redirect or wotnot.
Dan Atkinson