Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I build a tabbed menu in ASP.NET MVC?

Tags:

asp.net-mvc

I would like to build a tabbed menu pretty similar to the profile management of StackOverflow.

tabbed menu StackOverflow http://img410.imageshack.us/img410/3037/image1nwr.jpg

When you take a look at the url, it said: /users/flesym?tab=stats or ?tab=prefs. I was able to create the tabbed menu, but I'd like to know HOW can I call an action method and display the result depending the selected tab.

I tried using a partial view. But as my page /users/flesym inherits from Mvc.ViewPage(myApplication.Models.User), I can't use another inheritance in my partial view (for example, I'd like to use Mvc.ViewUserControl(myApplication.Models.Format)).

Any thoughts on how to do it ?

like image 697
Flesym Avatar asked Jun 10 '09 20:06

Flesym


2 Answers

Create View Model:

public class UserViewModel {
    public myApplication.Models.User User;

    public string PartialViewName;

    public PartialViewModelBase Tab;
}

Create View Models for each Tab, derived from PartialViewModelBase:

public abstract class PartialViewModelBase {
}

public class Tab1PartialViewModel : PartialViewModelBase {
    ...
}

public class TabNPartialViewModel : PartialViewModelBase {
    ...
}

Then make your View and PartialViews strongly typed:

View:

<%@ Page Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<UserViewModel>" %>

PartialViews:

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<Tab1PartialViewModel>" %>

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<TabNPartialViewModel>" %>

Then in your View you can use your Partial Views as:

<% Html.RenderPartial(Model.PartialViewName, Model.Tab); %>

In your controller action:

public ActionResult YourAction(string tab)
{
    // check if tab is valid !!!

    var model = new UserViewModel {
        User = new myApplication.Models.User();
        PartialViewName = tab;
        Tab = TabRepository.GetTabByName(tab);
        /*
         * or
         * Tabs = (new Dictionary<string, type> {
         *     {"Tab1", typeof(Tab1PartialViewName)},
         *     {"TabN", typeof(TabNPartialViewName)}
         *     })[tab];
         */
    };

    Return View(model);
}

Hope this helps.

like image 198
eu-ge-ne Avatar answered Oct 15 '22 08:10

eu-ge-ne


they are likely using the jquery ui tabs: http://docs.jquery.com/UI/Tabs

like image 28
Joel Martinez Avatar answered Oct 15 '22 09:10

Joel Martinez