在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 ...
随机推荐
- 01HTTP服务&AJAX编程
HTTP服务&AJAX编程 一.服务器 1. 什么是服务器? 能够提供某种服务的机器(计算机)称为服务器. 2.服务器的分类: 1.按系统分类:Lin ...
- HTTP权威协议笔记-6.代理
6.1 Web的中间实体 Http的代理服务器即是客户端的服务器又是服务器的客户端. 它介于服务器与客户端之间,当客户端发送请求报文经过它时,它会像服务器一样正确的处理请求和返回响应,同时,代理服务器 ...
- Php安全规范
一.基本准则 所有的用户输入都是有害的,对所有从客户端传入的数据都不信任, 需要做判断和过滤(类型,长度,格式,范围),否则可能会受到SQL Injection.XSS等攻击.比如:$_GET, $_ ...
- XidianOJ 1044 炸金花
题目描述 炸金花是一个风靡全球的扑克游戏,不少人因为这个游戏发了家,而更多的人则输得倾家荡产.为了帮助赌徒们戒掉它,现在决定派你去写一个程序,帮助赌徒们更好的认识这个游戏. 炸金花在这里被简化成这样一 ...
- 微软ASP.NET技术“乱谈”
微软ASP.NET技术“乱谈” 2014新年了,顺手写的一点文字,主要谈谈我对当前微软ASP.NET技术的看法,比较随意,大伙儿随便看看吧. 1 当前微软Web平台技术全貌 从2002年发布.NET ...
- Sql Server中不常用的表运算符之UNPIVOT
在Sql Server中不常用的表运算符之PIVOT中,介绍了PIVOT表运算符,现在来说说与之相对应的另一个表运算符UNPIVOT. 从名字可以看出,这个运算符的作用与PIVOT刚好相反,是将一行的 ...
- dfs 翻棋盘end
#include<iostream> char data[16]; int a[16]; int d[4] = { -4, 1, 4, -1 }; char b[16]; int flag ...
- python之路-Day10
操作系统发展史介绍 进程.与线程区别 python GIL全局解释器锁 线程 语法 join 线程锁之Lock\Rlock\信号量 将线程变为守护进程 Event事件 queue队列 生产者消费者模型 ...
- iOS有用的三方库
DKNightVersion https://github.com/Draveness/DKNightVersion#podfile 用来为APP添加夜间模式和换肤功能
- Breakpoint is not hit
新拿到一个Silverlight项目,能够正常运行,但是一旦运行起来,断点处由实心点变成了空心的,并警告:The breakpoint will not currently be hit. No sy ...