在ASP.NET MVC中使用Juqery實現頁面局部重新整理

來源:互聯網
上載者:User
自己做的一個實驗,留作備忘,此執行個體包括擴一下幾個檔案:
1、MyMovieController.cs
2、Index.aspx
3、ViewUserControl1.ascx
4、movie類
其中MyMovieController.cs不用再說了,代碼如下
MyMovieController.csusing 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 ?? 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.csusing 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("見龍卸甲","動作片")                };            }        }    }}

 
ViewUserControl1.ascx<%@ 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 = 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 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>


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.