MVC4 Ajax under AspNet MVC/using jquery to make related AJAX requests

Source: Internet
Author: User

Source Code reference: Link: http://pan.baidu.com/s/1pKhHHMj Password: MKR4

1: New--Project-->web-->asp.net MVC 4 Web application. Named as: Mvc4jqueryajaxdemo

2: New controller: Right-click on the Controllers folder and add the controller, named: HomeController (HomeController. cs)

3: Added Action:getdate () in controller HomeController

1 usingSystem;2 usingSystem.Collections.Generic;3 usingSystem.Linq;4 usingsystem.web;5 usingSYSTEM.WEB.MVC;6 7 namespacemvc4jqueryajaxdemo.controllers8 {9      Public classHomecontroller:controllerTen     { One         // A         //GET:/home/ -  -          Publicactionresult Index () the         { -             returnView (); -         } -  +          Publicactionresult GetDate () -         { +             returnContent (DateTime.Now.ToString ()); A         } at  -     } -}
HomeController.cs

4: New View: Right--add view name on HomeController action:index: Index (Default and Action name consistent) (index.cshtml)

5: Add the following code to the view index, using the AJAX request script provided by Microsoft, as follows:

5.1: Add Reference to Jquery-1.8.2.min.js

1 @{2 Layout = null;3 }4 5 <!DOCTYPE HTML>6 7 <HTML>8 <Head>9     <Metaname= "Viewport"content= "Width=device-width" />Ten     <title>Index</title> One     <Scriptsrc= "~/scripts/jquery-1.8.2.min.js"></Script> A     <Script> -         $(function () { -             $("#btnJQ"). Click (function () { the $.ajax ({ - URL:"/home/getdate", - Type:"POST", - Success:function(data) {alert (data);}, + Data:"id=111&name=222" //test Data -                 }); +  A                 //another Ajax post at $.post ("/home/getdate", "", function(data) {alert (data);}); -             }); -         }); -     </Script> - </Head> - <Body> in     <Div> -         <H1>MVC 4 JQuery Ajax Demo</H1> to         <inputtype= "button"ID= "BTNJQ"name= "BTNJQ"value= "Getserverdate" /> +     </Div> - </Body> the </HTML>
index.cshtml

6: Compile, Run page default is/home/index

6.1: Click getserverdate button to appear the following page

MVC4 Ajax under AspNet MVC/using jquery to make related AJAX requests

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.