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; }); });
HomeController.cs
public class HomeController : Controller { [HttpGet] public ActionResult Details(int id) { //write logic here to get data return View(); } }
- 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>
<script src="~/Scripts/jquery-1.10.2.min.js"></script>
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