Use juqery in ASP. net mvc for partial page refreshing

Source: Internet
Author: User
I made an experiment by myself and left it as a memo. This example includes the following files:
 
1. mymoviecontroller. CS
 
2. Index. aspx
 
3. viewusercontrol1.ascx
 
4. Movie class
 
Here, mymoviecontroller. CS does not need to be mentioned,CodeAs follows:
 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 ?? 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; 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 (" Long xingtianxia "," Action movies "), New Movie (" Hu Dan longwei "," Action movies "), New Movie (" Long Humen "," Action movies "), New Movie (" Menglong crossing the river "," Action movies "), New Movie (" Descendant of Dragon "," Action movies "), New Movie (" Battle of Dragon "," Action movies "),New Movie (" Tie Jia weilong "," Action movies "), New Movie (" See dragon unload "," Action movies ")};}}}}

 
 
 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> <% = 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 "> // Send an asynchronous request and output the result to <Div id = "result"> </div>          // The last parameter can be HTML or text" Function search (query, page) {$. Post (" /Mymovie/search "," Query = "+ Query +" & Page = "+ Page, function (data) {$ (" # Result "Pai.html (data); $ (" # Result table tbody TR: odd "Developer.css (" Background ","# F5deb3 ");}," Text "); // Shield the hyperlink jump              Return   False ;} $ (Function (){ // Bind an event to the search button $ (" # Search "). Click (function () {search ($ (" # Query "). Val ());}) // Bind a click event to the newly generated paging connection $ (" A "). Live (" Click ", Function () {search ($ (" # Query "). Val (), $ ( This ). ATTR (" Title ") ;};}) </SCRIPT>  Query ") %> <Input type =" Button "Id =" Search " Value =" Submit "/> <Div id =" Result "> </Div> </body> 


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.