• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

海洋工作室——网站建设专家:jQuery-UiTabsandAsp.netMVC

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

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

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


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
ASP.NETMVC源代码剖析发布时间:2022-07-10
下一篇:
【ASP.NET基础】客户端、服务器端的数据验证+CKEditer发布时间:2022-07-10
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap