Ocean studio-website construction expert: jquery-UI tabs and Asp.net MVC

Source: Internet
Author: User

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 ?
  1. <Div ID="Tabs">
  2. <Ul>
  3. LI > A / span> href = " # tabs-1 " text 1 A > LI >
  4. LI A href = "# tabs-2" text 2 A > LI / span> >
  5. <Li><A Href="# Tabs-3">Text 3</A></Li>
  6. </Ul>
  7. <Div ID="Tabs-1">
  8. <% Html. renderpartial ("_ tab1", model); %></Div>
  9. <Div ID="Tabs-2">
  10. <% Html. renderpartial ("_ tab2", model); %></Div>
  11. <Div ID="Tabs-3">
  12. <% Html. renderpartial ("_ tab3", model); %></Div>
  13. </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 ?
  1. PublicActionresult getajaxtab (IntID)
  2. {
  3. StringViewname =String. Empty;
  4. Tabexample. Services. tabtextservice serv =NewTabexample. Services. tabtextservice ();
  5. Tabviewmodel mymodel = Serv. gettabviewmodel ();
  6. Switch(ID)
  7. {
  8. Case1:
  9. Viewname ="_ Tab1";
  10. Break;
  11. Case2:
  12. Viewname ="_ Tab2";
  13. Break;
  14. Case3:
  15. Viewname ="_ Tab3";
  16. Break;
  17. Default:
  18. Viewname ="_ Error";
  19. Break;
  20. }
  21. System. Threading. thread. Sleep (1000 );
  22. ReturnPartialview (viewname, mymodel );
  23. }
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 ?
  1. FunctionGetcontenttab (INDEX ){
  2. VaRUrl ='<% = URL. Content ("~ /Home/getajaxtab ") %> /'+ Index;
  3. VaRTargetdiv ="# Tabs -"+ Index;
  4. VaRAjaxloading ="~ /Content") %/>/Ajax-loader.gif 'align = 'left' Height = '28' width = '28'>";
  5. Certificate (targetdiv).html ("
  6. "+ Ajaxloading +"Loading...
  7. ");
  8. $. Get (URL,Null,Function(Result ){
  9. Vertex (targetdiv).html (result );
  10. });
  11. }
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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.