tags:

views:

96

answers:

3

Hi All,

Trying to create a strongly typed master page with multi level navigation and would love to hear your opinion.

i'm using the sample recommended by MS here: http://www.asp.net/mvc/tutorials/passing-data-to-view-master-pages-vb

so i have an ApplicationController that gets all the categories and all the other controller inherits it. and it return a LIST and stores it in ViewData["Nav"]

The master page as a partial view that gets the NAV model and creates the menu. The roues for the category Category/{CategoryId}/{CategoryName}/{Page}

The question is how can i display the selected category or sub category as selected when i renders it inside the partialView.

I see some options: 1. Create another property in the applicatin controller :

    public class CategoryController : AppliactionController
{
    //
    // GET: /Category/

    public ActionResult Index(string categoryId, string categoryName, int page)
    {
        base.ActiveCategoryId=int.parse(categoryId);
        return View();
    }
  1. Check the current action URL in the partial view when creating the menu and set the category as selected if it produces the same action URL (not sure if i can get the categoryid from the action)

Any suggestions?

A: 

If you're going to use the Master Controller pattern, then option 1 is a suitable solution. For even better separation, you may consider moving that logic in the action into an action filter.

I would avoid option 2 completely, because you don't want that kind of logic in your view.

A third option you can try is to not use the Master Controller pattern, and instead set up your view to call RenderActions on stuff that is orthogonal to the view's main concern. In this case, your view would look something like Html.RenderAction("Menu", Model.CurrentCategoryId)

Martin Aatmaa
Hi,Thanks for the quick reply, Can you specify more regarding moving the logic to an action filter? (Migrating from WebForms..)Thanks again!
tomer
A: 

Someone asked a similar question few days ago here

mare
Regarding your sugestion: "you may consider moving that logic in the action into an action filter."I could do that but is it possible to access the controller case controller from the action filter?should it be something likepublic void OnActionExecuting(ActionExecutingContext filterContext) { ((AppController)filterContext.Controller.base)).ActiveCategoryId=int.parse( filterContext.ActionParameters["CategoryId"])Didn't check the code, just wanted to hear your thoughts, is this what you suggested?Thanks
tomer
A: 

Hi Martin.

Regarding your seggestion: "you may consider moving that logic in the action into an action filter."

I could do that but is it possible to access the controller case controller from the action filter?

should it be something like

public void OnActionExecuting(ActionExecutingContext filterContext)
{
    ((AppController)filterContext.Controller.base)).ActiveCategoryId=int.parse( filterContext.ActionParameters["CategoryId"])

Didn't check the code, just wanted to hear your thoughts, is this what you suggested?

Thanks

TomerMiz