How to use AJAX with ASP.NET MVC

Last modified: July 04, 2021
Step 1

Create an ASP.NET MVC application.

createproject createproject createproject

In Home Controller, create a method called SayHello that takes name return a JSON (contain "Hello + input name" )

[HttpPost] public JsonResult SayHello(string name) { return Json("Hello " + name); }

In the Home View of Home Controller, remove the content and put below

<button type="button" onclick="callAJAX()">Say Hello</button> <script type="text/javascript"> //simple AJAX Call function callAJAX() { $.ajax({ method: "POST", url: "/Home/SayHello", data: { name: 'Mark' } }) .done(function (msg) { alert(msg); }); } </script>