How to realize right-click menu and simple paging in ASP.NET MVC 2
This article shows you how to achieve right-click menu and simple pagination in ASP.NET MVC 2, concise and easy to understand, absolutely can make you shine, through the detailed introduction of this article I hope you can gain something.
Right-click menus are very convenient and often used. This article will use a JQUERY plugin to implement right-click menus in ASP.NET MVC. This article also describes how to implement simple pagination in ASP.NET MVC. The effect is as follows:
First, download this plugin.
Create a new ASP.NET mvc application. Place this plug-in in the Scripts folder. and reference it on the page.
This demo uses the Product table to the NORTHWND database.
Define right-click menu:
detail
new
delete
modify
将此菜单定义在产品名上,故在在产品名上添加一个class供jquery选择。
在页面上插入下面脚本。用于绑定菜单项的行为。为了简单起见,将所以的菜单项的行为都定义成导航到详情页面.
$(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; } } }); });
这样就非常简单的实现了右键菜单的功能。
下面说下实现简单的分页。asp.net mvc中分页非常简单。
看下面定义的table的html代码:
ProductName SupplierID CategoryID11 QuantityPerUnit UnitPrice UnitsInStock20 UnitsOnOrder23 ReorderLevel Discontinued
我们只要在这个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("Previous", urlPrefix, currentPage)); if (currentPage - currentPageSize >= 0) sb1.AppendLine(String.Format("...", urlPrefix, (currentPage - currentPageSize) + 1)); for (int i = seed; i < Math.Round((totalRecords / 10) + 0.5) && i < seed + currentPageSize; i++) { sb1.AppendLine(String.Format("{1}", urlPrefix, i + 1)); } if (currentPage + currentPageSize