现在的位置: 首页 > 综合 > 正文

海洋工作室——网站建设专家:jQuery-Ui Tabs and Asp.net MVC

2013年06月08日 ⁄ 综合 ⁄ 共 4364字 ⁄ 字号 评论关闭

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 would use asp.net mvc with jQuery-UI’s tabs. I have slapped together an example project that shows how you could implement the tabs in an asp.net mvc web site. I created an ajax and a regular tab example.

Tab Example jQueryUi

Tab Example jQueryUi

Basically I use a partial view for each tab. This partial view is then also used for the ajax response.

  1.   
  2. <div id="tabs">  
  3. <ul>  
  4.     <li><a href="#tabs-1">Text 1</a></li>  
  5.     <li><a href="#tabs-2">Text 2</a></li>  
  6.     <li><a href="#tabs-3">Text 3</a></li>  
  7. </ul>  
  8. <div id="tabs-1">  
  9.             < % Html.RenderPartial("_tab1", Model);  %></div>  
  10. <div id="tabs-2">  
  11.             < % Html.RenderPartial("_tab2", Model);  %></div>  
  12. <div id="tabs-3">  
  13.             < % Html.RenderPartial("_tab3", Model);  %></div>  
  14. </div>  

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 would 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 could get away with this in this example ;) .

  1.   
  2.         public ActionResult getAjaxTab(int id)   
  3.         {   
  4.             string viewName = string.Empty;   
  5.   
  6.             TabExample.Services.tabTextService serv = new TabExample.Services.tabTextService();   
  7.             tabViewModel myModel = serv.getTabViewModel();   
  8.   
  9.             switch (id)   
  10.             {   
  11.                 case 1:   
  12.                     viewName = "_tab1";   
  13.                     break;   
  14.                 case 2:   
  15.                     viewName = "_tab2";   
  16.                     break;   
  17.                 case 3:   
  18.                     viewName = "_tab3";   
  19.                     break;   
  20.                 default:   
  21.                     viewName = "_error";   
  22.                     break;   
  23.             }   
  24.   
  25.             System.Threading.Thread.Sleep(1000);      
  26.   
  27.             return PartialView(viewName, myModel);   
  28.         }  

I am using this js function to update the tabs.

  1.   
  2.         function getContentTab(index) {   
  3.             var url='< %= Url.Content("~/Home/getAjaxTab") %>/' + index;   
  4.             var targetDiv = "#tabs-" + index;   
  5.             var ajaxLoading = "<img id='ajax-loader' src='<%= Url.Content("~/Content") %/>/ajax-loader.gif' align='left' height='28' width='28'>";   
  6.   
  7.             $(targetDiv).html("   
  8. " + ajaxLoading + " Loading...   
  9. ");    
  10.   
  11.             $.get(url,nullfunction(result) {   
  12.                 $(targetDiv).html(result);   
  13.             });   
  14.         }  

There is a select event on 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

抱歉!评论已关闭.