This is a simple tutorial to explain how to use jquery UI tab in MVC3 to create tabview in MVC. MVC does not support server side ASP.NET control like ASP.NET. MVC3 specially contains HTML helper class and Ajax class to support UI design. There is no helper class method that will provide tabview for the UI. In this tutorial, I will demonstrate how to create tabview in MVC3 using jquery UI tab plugin. I have not explained how to use jquery UI tab plugin because that is done better in jquery side. I request you to download jquery plugin and read how to use the plugin from here.
Create MVC3 empty project by selecting view engine as the Razor view. Add Home Controller and Index Action. I have Database name Demo in my App_Data folder. For demo purposes, I have added
Product table in my database.
Product table contains the product id, product name and product description as follows:
Cost up to 3 thousand. Manufacture product a and Product b.
Cost up to 500 Rs. Contain gel and ink pen.
Cost up to 100 Rs. Contain 4,3,2,1 B pencile.
Cost up to 200 Rs. Contain 200 pages and 400 pages book.
My intention is to create a simple Tab view where product name is defined as the tab and product description is defined as the tab content.
Using the Code
Let's start building our
TabView using Jquery UI Tab. I have created ADO.NET entity data model named Demo.edmx as my
Model in Model folder of MVC project.
I have instantiated
Model entity class as follows:
DemoEntities db = new DemoEntities();
Let's fetch all records from
product table and pass the records to the view.
public ActionResult Index()
IList<Product> productList = new List<Product>();
productList = (from m in db.Products select m).ToList<Product>();
I have created
Index action in Home controller. I have added Linq expression to fetch record from
product table and type cast to List of product and pass that list to the view.
All set now, let's create
TabView with just a two step process.
Step 1: For each tab, create list item having
anchor tag with unique id prefix with '#' representing caption of the tab.
Step 2: For each list item, add
div with same id as that of
anchor tag id representing content of the tab.
Div id is not prefixed with '#'.
Note: For each tab
anchor tag and
div tag, id is the same but
anchor tag id is prefixed with
div tag id is not.
Rest of the magic is done by the JQuery UI Tab.
I have created strongly typed Index view of typeof
IEnumerable collection of
product entity class and pass the list of
product entity class to the View.
ViewBag.Title = "Index";
@foreach (var item in Model)
@foreach (var item in Model)
I have set
product name as my tab caption and
product description as my tab content. Tab caption is represented by the link name. Tab content is represented by the
div elements having the same id represent a tab.
Note: Link id prefix with
div id is not. I have a unique product name in my
product table. I have kept
product name as link and
div id where link id prefix with #.
Please confirm that you have added the following JQuery and CSS.
We have selected the
div with id tabs as our tabview container which contains un-ordered list with
anchor tag as tab caption and
div tag as tab content that will be converted into tabview. Rest of the magic is done by the jquery UI tab plugin. You can customize your tab view based on your requirement by modifying CSS "content/themes/base/jquery-ui.css". This ends our simple demo of tabview. Hope you enjoyed this demo.