当前位置: 移动技术网 > IT编程>开发语言>.net > ASP.NET MVC 2右键菜单和简单分页实例讲解

ASP.NET MVC 2右键菜单和简单分页实例讲解

2017年12月12日  | 移动技术网IT编程  | 我要评论

亿融卡盟,白话灵犀,薛家燕个人资料

右键菜单非常方便,很多时候会用到。这篇文章将使用一个jquery的插件在asp.net mvc中实现右键菜单。本文还将介绍一下在asp.net mvc中如何实现简单的分页。效果如下图:

新建一个asp.net mvc应用程序。将此插件放入scripts文件夹。并在页面上引用。
定义右键菜单:

<div class="contextmenu" id="mymenu1"> <ul> 
<li id="detail">
<img src="http://www.cnblogs.com/content/detail.ico" />detail</li> 
<li id="new"><img src="http://www.cnblogs.com/content/new.ico" />new</li>
<li id="delete"> 
<img src="http://www.cnblogs.com/content/delete.ico"/>delete</li> 
<li id="modify">
<img src="http://www.cnblogs.com/content/modify.ico"/>modify</li> 
 </ul> </div> 

将此菜单定义在产品名上,故在在产品名上添加一个class供jquery选择。

<td class="showcontext" id="<%= item.productid %>">
<%: item.productname %></td> 

在页面上插入下面脚本。用于绑定菜单项的行为。为了简单起见,将所以的菜单项的行为都定义成导航到详情页面.

<script type="text/javascript"> 
 $(document).ready(function () { 
  $('td.showcontext').contextmenu('mymenu1', { 
   bindings: { 
    'detail': function (t) { 
   document.location.href = '/products/detail/'+t.id; 
    }, 
    'new': function (t) { 
   document.location.href = '/products/detail/' + t.id; 
    }, 
     'delete': function (t) { 
      confirm("你确定删除吗?"); 
   document.location.href = '/products/detail/' + t.id; 
    }, 
     'modify': function (t) { 
  document.location.href = '/products/detail/' + t.id; 
    } 
    } 
  }); 
  }); 
</script> 

这样就非常简单的实现了右键菜单的功能。

下面说下实现简单的分页。asp.net mvc中分页非常简单。

看下面定义的table的html代码:

 <table> 
 <tr> 
   <th> 
    productname 
    </th> 
   <th> 
    supplierid 
   </th> 
   <th> 
    categoryid11    </th> 
   <th> 
     quantityperunit 
   </th> 
   <th> 
     unitprice 
   </th> 
    <th> 
    unitsinstock20    </th> 
   <th> 
     unitsonorder23    </th> 
    <th> 
    reorderlevel 
   </th> 
   <th> 
    discontinued 
    </th> 
   </tr> 
 <% foreach (var item in model.products) 
  { %> 
  <tr> 
 <td class="showcontext" id="<%= item.productid %>"> 
<%: item.productname %></td> 
    <td> 
     <%: item.supplierid %> 
   </td> 
    <td> 
    <%: item.categoryid %> 
   </td> 
    <td> 
    <%: item.quantityperunit %> 
    </td> 
    <td> 
  <%: string.format("{0:f}", item.unitprice) %> 
   </td> 
    <td> 
    <%: item.unitsinstock %> 
    </td> 
   <td> 
    <%: item.unitsonorder %> 
    </td> 
   <td> 
   <%: item.reorderlevel %> 
   </td> 
   <td> 
    <%: item.discontinued %> 
   </td> 
  </tr>  
 <% } %> 
</table> 

我们只要在这个table下面插入一段分页的html脚本就行了。分页的脚本当然要生成,使用htmlhelper的扩展方法去生成这个脚本。看下面的扩展方法,非常的简单的生成了分页的html代码:

public static string pager(this htmlhelper helper, int currentpage, int currentpagesize, int totalrecords, string urlprefix) 
  { 
   stringbuilder sb1 = new stringbuilder(); 
int seed = currentpage % currentpagesize == 0 ? currentpage : currentpage - (currentpage % currentpagesize); 
if (currentpage > 0) 
sb1.appendline(string.format("<a href=\"{0}/{1}\">previous</a>", urlprefix, currentpage)); 
if (currentpage - currentpagesize >= 0) 
sb1.appendline(string.format("<a href=\"{0}/{1}\">...</a>", urlprefix, (currentpage - currentpagesize) + 1)); 
for (int i = seed; i < math.round((totalrecords / 10) + 0.5) && i < seed + currentpagesize; i++) 
 { 
sb1.appendline(string.format("<a href=\"{0}/{1}\">{1}</a>", urlprefix, i + 1)); 
 } 
if (currentpage + currentpagesize <= (math.round((totalrecords / 10) + 0.5) - 1)) 
sb1.appendline(string.format("<a href=\"{0}/{1}\">...</a>", urlprefix, (currentpage + currentpagesize) + 1)); 
if (currentpage < (math.round((totalrecords / 10) + 0.5) - 1)) 
sb1.appendline(string.format("<a href=\"{0}/{1}\">next</a>", urlprefix, currentpage + 2)); 
return sb1.tostring(); 
} 


然后在table后面添加下面的代码,在table下面输出分页的html代码:

<div class="pager"> 
<%=html.pager(model.currentpage, model.totalpages,model.totalitems ,"/products/list")%>
 </div> 


这样就完成分页和右键菜单的功能了。是不是非常的简单呢。:)

效果:

显示:

通过一个插件实现asp.net mvc 2中的右键菜单和一个相当简单的分页,希望能够帮助到大家熟练掌握分页功能的实现。

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网