jQuery Ajax GET Request with Input Parameter in ASP.NET MVC

In many forum post, I have read one common question that is how to make jQuery ajax GET request with input parameter in ASP.NET MVC. So by considering the above requirement, I have decided to write this article.
Let's consider the scenario in which we have an action method named Details in Home controller which takes the Id as an input parameter. Then the jQuery function will 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 the Home controller has an action result method named Details which takes id as an 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 the jQuery library. You can use the 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 the above jQuery library, you need an active internet connection. You can download and use it as an offline. You need to add reference as below.
<script src="~/Scripts/jquery-1.10.2.min.js"></script>
From the preceding example we have learned how to make jQuery Ajax GET request with input parameter in ASP.NET MVC.

Summary

I hope this article is useful for all readers. If you have a suggestion related to this article, then please contact me.

Post a Comment

www.CodeNirvana.in

Protected by Copyscape Online Copyright Protection
Copyright © Compilemode