在ASP.NET MVC中使用Juqery实现页面局部刷新
自己做的一个实验,留作备忘,此实例包括扩一下几个文件:
1、MyMovieController.cs
2、Index.aspx
3、ViewUserControl1.ascx
4、movie类
其中MyMovieController.cs不用再说了,代码如下
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Mvc.Ajax; namespace MyMVC.Controllers
{
public class MYMovieController : Controller
{
//
// GET: /MYMovie/ public ActionResult Index()
{
return View();
}
public ActionResult Search(string query, int? page)
{
List<movie> movies = movie.Movies
.Where(r => r.MovieName.Contains(query))
.OrderByDescending(r => r.MovieName)
.Skip((page ?? ) * )
.Take()
.ToList();
if (Request.IsAjaxRequest())
{
int moiveCount=movie.Movies.Where(r => r.MovieName.Contains(query)).Count();
ViewData["totalPage"] = (int)Math.Ceiling(moiveCount / 4d);
ViewData["query"] = query;
return View("ViewUserControl1", movies);
}
else
{
return View();
}
} }
}
MyMovieController.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web; namespace MyMVC
{
public class movie
{
public string MovieName { get; set; }
public string Category { get; set; } public movie(string movieName, string category)
{
this.MovieName = movieName;
this.Category = category;
} public static List<movie> Movies
{
get
{
return new List<movie>
{
new movie("龙行天下","动作片"),
new movie("虎胆龙威","动作片"),
new movie("龙虎门","动作片"),
new movie("猛龙过江","动作片"),
new movie("龙的传人","动作片"),
new movie("龙之战","动作片"),
new movie("铁甲威龙","动作片"),
new movie("见龙卸甲","动作片")
};
}
}
}
}
movie.cs
<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<IEnumerable<MyMVC.movie>>" %> <table>
<thead>
<tr>
<th>
MovieName
</th>
<th>
Category
</th>
</tr>
</thead>
<tbody>
<% foreach (var item in Model) { %> <tr>
<td>
<%= Html.Encode(item.MovieName) %>
</td>
<td>
<%= Html.Encode(item.Category) %>
</td>
</tr> <% } %>
</tbody> </table> <p>
<%
int totalPage = (int)ViewData["totalPage"];
string query = ViewData["query"].ToString();
for (var i = ; i < totalPage; i++)
{
%>
<a href="#" title="<%=i %>"><%= Html.Encode(i+) %></a>
<%
}
%>
</p>
ViewUserControl1.ascx
<%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Index</title>
<style type="text/css">
#result table thead tr
{
background-color:#cccccc;
}
</style>
<script src="../../Scripts/jquery-1.4.1-vsdoc.js" type="text/javascript"></script>
<script type="text/javascript">
//发送异步请求,将结果输出到<div id="result"></div>中
//最后一个参数可以是"html"也可以是"text"
function search(query, page)
{
$.post("/MYMovie/Search", "query=" + query + "&page=" + page, function(data)
{
$("#result").html(data);
$("#result table tbody tr:odd").css("background", "#F5DEB3");
}, "text"
);
//屏蔽超级链接跳转
return false;
} $(function()
{
//为搜索按钮绑定事件
$("#search").click(function()
{
search($("#query").val());
})
//为新生成的分页连接绑定click事件
$("a").live("click", function()
{
search($("#query").val(), $(this).attr("title"));
});
})
</script>
</head>
<body>
<div>
<h2>搜索电影</h2>
<%= Html.TextBox("query") %>
<input type="button" id="search" value="提交" />
<div id="result"></div>
</div>
</body>
</html>
Index.aspx
在ASP.NET MVC中使用Juqery实现页面局部刷新的更多相关文章
- 2.ASP.NET MVC 中使用Crystal Report水晶报表
上一篇,介绍了怎么导出Excel文件,这篇文章介绍在ASP.NET MVC中使用水晶报表. 项目源码下载:https://github.com/caofangsheng93/CrystalReport ...
- 关于 ASP.NET MVC 中的视图生成
在 ASP.NET MVC 中,我们将前端的呈现划分为三个独立的部分来实现,Controller 用来控制用户的操作,View 用来控制呈现的内容,Model 用来表示处理的数据. 从控制器到视图 通 ...
- 在Asp.Net MVC 中配置 Serilog
Serilog 是一种非常简便记录log 的处理方式,使用Serilog可以生成本地的text文件, 也可以通过 Seq 来在Web界面中查看具体的log内容. 接下来就简单的介绍一下在Asp.Net ...
- 如何在 ASP.NET MVC 中集成 AngularJS(3)
今天来为大家介绍如何在 ASP.NET MVC 中集成 AngularJS 的最后一部分内容. 调试路由表 - HTML 缓存清除 就在我以为示例应用程序完成之后,我意识到,我必须提供两个版本的路由表 ...
- 如何在 ASP.NET MVC 中集成 AngularJS(2)
在如何在 ASP.NET MVC 中集成 AngularJS(1)中,我们介绍了 ASP.NET MVC 捆绑和压缩.应用程序版本自动刷新和工程构建等内容. 下面介绍如何在 ASP.NET MVC 中 ...
- 《Entity Framework 6 Recipes》中文翻译系列 (20) -----第四章 ASP.NET MVC中使用实体框架之在MVC中构建一个CRUD示例
翻译的初衷以及为什么选择<Entity Framework 6 Recipes>来学习,请看本系列开篇 第四章 ASP.NET MVC中使用实体框架 ASP.NET是一个免费的Web框架 ...
- asp.net mvc 中 一种简单的 URL 重写
asp.net mvc 中 一种简单的 URL 重写 Intro 在项目中想增加一个公告的功能,但是又不想直接用默认带的那种路由,感觉好low逼,想弄成那种伪静态化的路由 (别问我为什么不直接静态化, ...
- 在 ASP.NET MVC 中充分利用 WebGrid (microsoft 官方示例)
在 ASP.NET MVC 中充分利用 WebGrid https://msdn.microsoft.com/zh-cn/magazine/hh288075.aspx Stuart Leeks 下载代 ...
- ASP.NET MVC中的两个Action之间值的传递--TempData
一. ASP.NET MVC中的TempData 在ASP.NET MVC框架的ControllerBase中存在一个叫做TempData的Property,它的类型为TempDataDictiona ...
随机推荐
- 【转】jqGrid 各种参数 详解
[原文]http://www.cnblogs.com/younggun/archive/2012/08/27/2657922.htmljqGrid 各种参数 详解 JQGrid JQGrid是一个 ...
- Modelica学习
Annotation Choices for Suggested Redeclarations and Modifications Replaceable model sample(start,int ...
- block的初识
block的介绍: Block是iOS4.0之后新增的一种语法结构,也称为“闭包(closure)”. SDK4.0新增的API大量使用了Block. Block是一个匿名的函数代码块,此代码 ...
- 发现IE7的一个问题,不能用索引取字符串中的单个字符
如下javascript: var testValue="hello,world"; alert(testValue[]); 在IE7上运行该代码,竟然提示值为"unde ...
- Struts2------通配符
<struts> <package namespace="/" extends="struts-default" name="tes ...
- lua面试基础知识
1.lua中八种基础类型:nil(空),boolean(布尔),number(数字),string(字符串),userdata(自定义类型),function(函数),thread(线程),table ...
- 【POJ2828】Buy Tickets(线段树)
题意:有一个输入序列,每次操作要把b[i]插入到第a[i]个,在第a[i]个后面的要后移,问最后序列. n<=200000 思路:顺序来只能用splay维护 考虑倒序,对于插入到第K个位置,在线 ...
- ajax将json写到table中去
查询条件: <table style="width: 100%;border-collapse: collapse;" > <tr> <th styl ...
- nginx + lua +redis环境搭建
环境搭建,其实主要是lua的环境,这个环境够麻烦的,在网上找了很多前辈的文章,终于完成了 ,安装redis wget http://download.redis.io/releases/redis-3 ...
- Ubuntu 杂音 alsa*
$ sudo alsactl init # 初始化 $ sudo alsactl store # 存储状态 会调的话可以 $ alsamixer