在ASP.NET MVC中使用Jquery实现页面局部刷新

需要文件

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.Mvc;
using MvcSimpleModelBinding.Models;

namespace MvcSimpleModelBinding.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 ?? 0) * 4)
                .Take(4)
                .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();
            }
        }

    }
}

Movie.cs

using System.Collections.Generic;

namespace MvcSimpleModelBinding.Models
{
    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("龙行天下21","动作片"),
                    new Movie("虎胆龙威","动作片"),
                    new Movie("龙虎门e","动作片"),
                    new Movie("猛龙过w江","动作片"),
                    new Movie("龙的传人","动作片"),
                    new Movie("龙之战w","动作片"),
                    new Movie("铁甲威龙","动作片"),
                    new Movie("见龙卸we甲","动作片"),
                    new Movie("龙行天下","动作片"),
                    new Movie("虎胆龙we威","动作片"),
                    new Movie("龙虎门","动作片"),
                    new Movie("猛龙过w江","动作片"),
                    new Movie("龙的传we人","动作片"),
                    new Movie("龙之战","动作片"),
                    new Movie("铁甲ds威龙","动作片"),
                    new Movie("见龙卸sd甲","动作片")
                };
            }
        }
    }
}

ViewUserControl1.ascx

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<IEnumerable<MvcSimpleModelBinding.Models.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 = 0; i < totalPage; i++)
        {
    %>
    <a href="#" title="<%=i %>">
        <%= Html.Encode(i+1) %></a>
    <% 
}
    %>
</p>


Index.aspx

<%@ 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 id="Head1" 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) {
            // alert(query);
            $.post("/MYMovie/Search", "query=" + query + "&page=" + page, function (data) {
                //alert(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>
 

如果看不到效果,注意ascx、aspx的命名空间是否写错了

在添加视图那里添加ascx文件


http://www.cnblogs.com/bjs007/archive/2010/04/03/1703584.html

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值