简体   繁体   中英

asp.net mvc navigation on master page best practices

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?

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 RenderAction s 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)

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM