How To Create jQuery Ajax GET Method In ASP.NET MVC

In this article we will learn how to create the jQuery Ajax GET function in ASP.NET MVC which will GET the data from server by calling MVC controller action result method.
Lest consider the scenario we have action method in named Details in Home controller which takes the Id as input parameter .Then jQuery function will be look like as follows.
$(document).ready(function () {
        $("#btnGetDetails").click(function () {
            $.ajax({
                //base address/controller/Action
                url: 'http://localhost:51163/Home/Details',
                type: 'GET',
                data: {
                    //Passing Input parameter
                    id: $('#txtId').val()
                },
                success: function (result) {
                   //write something
                },
                error: function () {
                    alert("error");
                }
            });
            return false;
        });
    });
Suppose following is the Home controller having action result method named Details which takes id as input parameter.
HomeController.cs
  public class HomeController : Controller
    {
        
        [HttpGet]
        public ActionResult Details(int id)
        {
        //write logic here to get data

        return View();
        }
    }
Note
  • To work with jQuery we need to reference of jQuery library .You can use following CDN jQuery library from any provider such as Microsoft,Google or jQuery .
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script> 
To use above jQuery library you need an active internet connection , You can download and use it as offline .You need to be add reference as like below.
<script src="~/Scripts/jquery-1.10.2.min.js"></script>
From preceding example we have learned how to create jQuery Ajax GET method in ASP.NET MVC to call controller action method.
Summary
I hope this article is useful for all readers to learn how to  create jQuery Ajax GET method in ASP.NET MVC, If you have a suggestion related to this article then please contact me.
 To learn ASP.NET MVC step by step please refer following articles

Post a Comment

www.CodeNirvana.in

Protected by Copyscape
Copyright © Compilemode