Here is the original, http://ericdotnet.wordpress.com/2009/03/17/jquery-ui-tabs-and-aspnet-mvc/
Clay made a comment on my previous post and asked how I wocould use Asp.net MVCJquery-ui's tabs. I have slapped together an example project that shows how you cocould implement the tabs in an Asp.net MVC web site. I created an Ajax and a regular tab example.
Tab example jqueryui
Basically I use a partial view for each tab. This partial view is then also used for the Ajax response.
View plain Copy to clipboard Print ?
- <Div ID="Tabs">
- <Ul>
- LI > A / span> href = " # tabs-1 " text 1 A > LI >
- LI A href = "# tabs-2" text 2 A > LI / span> >
- <Li><A Href="# Tabs-3">Text 3</A></Li>
- </Ul>
- <Div ID="Tabs-1">
- <% Html. renderpartial ("_ tab1", model); %></Div>
- <Div ID="Tabs-2">
- <% Html. renderpartial ("_ tab2", model); %></Div>
- <Div ID="Tabs-3">
- <% Html. renderpartial ("_ tab3", model); %></Div>
- </Div>
<Div id = "tabs"> <ul> <li> <a href = "# tabs-1"> text 1 </a> </LI> <li>
There is no database access in the example project, I sort of simulated a database by using a service model that provides the viewmodel that holds the texts that are displayed in the three tabs.
Regarding the Ajax Controller method, it wocould of course be better to not get the entire viewmodel in a real world situation and then pass that on to the view, but since this is a brief demo I thought I cocould get away with this in this example.
View plain
Copy to clipboard
Print
?
-
-
- PublicActionresult getajaxtab (IntID)
-
- {
- StringViewname =String. Empty;
-
-
- Tabexample. Services. tabtextservice serv =NewTabexample. Services. tabtextservice ();
-
- Tabviewmodel mymodel = Serv. gettabviewmodel ();
-
- Switch(ID)
-
- {
-
- Case1:
-
- Viewname ="_ Tab1";
-
- Break;
- Case2:
-
- Viewname ="_ Tab2";
-
- Break;
-
- Case3:
- Viewname ="_ Tab3";
-
- Break;
-
- Default:
-
- Viewname ="_ Error";
- Break;
-
- }
-
-
- System. Threading. thread. Sleep (1000 );
-
-
- ReturnPartialview (viewname, mymodel );
-
- }
Public actionresult getajaxtab (int id) {string viewname = string. empty; tabexample. services. tabtextservice serv = new tabexample. services. tabtextservice (); tabviewmodel mymodel = Serv. gettabviewmodel (); Switch (ID) {Case 1: viewname = "_ tab1"; break; Case 2: viewname = "_ tab2"; break; Case 3: viewname = "_ tab3"; break; default: viewname = "_ error"; break;} system. threading. thread. sleep (1000); Return partialview (viewname, mymodel );}
I am using this JS function to update the tabs.
View plain
Copy to clipboard
Print
?
-
-
- FunctionGetcontenttab (INDEX ){
-
- VaRUrl ='<% = URL. Content ("~ /Home/getajaxtab ") %> /'+ Index;
- VaRTargetdiv ="# Tabs -"+ Index;
-
- VaRAjaxloading ="~ /Content") %/>/Ajax-loader.gif 'align = 'left' Height = '28' width = '28'>";
-
-
- Certificate (targetdiv).html ("
- "+ Ajaxloading +"Loading...
-
- ");
-
-
- $. Get (URL,Null,Function(Result ){
-
- Vertex (targetdiv).html (result );
-
- });
-
- }
Function getcontenttab (INDEX) {var url = '<% = URL. Content ("~ /Home/getajaxtab ") %>/'+ index; var targetdiv =" # tabs-"+ index; vaR ajaxloading = "/ajax-loader.gif 'align = 'left' Height = '28' width = '28'>"; Parameters (targetdiv+.html ("" + ajaxloading + "loading... "); $. get (URL, null, function (result) {detail (targetdiv).html (result );});}
There isSelect eventOn the tabs that you can hook into as well, I just find it easier to just use the onclick event of the tab Div's.
Hope this is some help to anybody out there, it was fun to create this small project. If anyone has any suggestions, or questions let me know in the comments!
Download tabexample Project Link