How to make a AJAX GET call in ASP.NET MVC application
Solution 1
Change your action method like this
[HttpGet]
public JsonResult Test()
{
return Json("myContent",JsonRequestBehavior.AllowGet);
}
And in your success method in 'html' variable you will get back "myContent".
Solution 2
Example :
function RemoveItem(ItemId) {
if (ItemId && ItemId > 0) {
$.ajax({
cache: false,
url: '@Url.Action("RemoveItem", "Items")',
type: 'POST',
data: { id: ItemId },
success: function (result) {
if (result.success == true) {
$("#CartItemGridDiv").load('@Url.Content("~/User/Items/CartItemGrid")');
alert('Item Removed successfully.');
}
else {
alert('Item not removed.');
}
},
error: function (result) {
alert('Item not removed.');
}
});
}
}
public ActionResult RemoveItem(int id)
{
if (id > 0)
{
bool addToChart = false;
addToChart = Utilities.UtilityClass.RemoveItem(id);
if (addToChart)
{
return Json(new { success = true }, JsonRequestBehavior.AllowGet);
}
}
return Json(new { success = false }, JsonRequestBehavior.AllowGet);
}
public ActionResult CartItemGrid()
{
List<CartItems> oItemList = (List<CartItems>)Session[MvcMAB.StringConst.CartItemSession];
return View(oItemList);
}
Unbreakable
The fool didn't know it was impossible, so he did it!
Updated on July 09, 2022Comments
-
Unbreakable almost 2 years
I am learning web development. And I just want to make a simple AJAX GET call in ASP.Net MVC Application and I want visualize how it works. But I am not able to do so. I am a beginner and I could have done any silly mistakes. But as of now there are no errors in my code.
Below is what I have:
So, I have a
Index.cshtml
file which is already loaded. Now in that page I want to make anAjax GET call
to one of thefunction
that I have written in theHomeController
and action name isTest
. I just want to hit the breakpoint in thatTest Action
of Homecontroller and return something back to theSuccess
of AJAX Call. In HomeController I have below Action[HttpGet] public ActionResult Test() { return View("hello"); }
jQuery
$.ajax({ url: '/Home/Test', type: 'GET', success: function (html) { alert(html); }, error: function (error) { $(that).remove(); DisplayError(error.statusText); } }); }
Confusion: Do I need to create a
cshtml
forTest
. But I actually do not want that. I just want theTest
Action to return me one data. And I will display that data in my Already openedIndex.csthml
file.Error: No error but I am not able to hit the breakpoint in
Test Action
of controller. Kindly note thatSuccess
of AJAX is hitting but I do not see any data. But I am sure it did not hit the test Action breakpoint. -
Unbreakable over 7 yearsI do not understand your answer. I just want to return one string. may be "hello world".
-
Unbreakable over 7 yearsWhat is this
int? id and myListQuery and all
. -
Ramy M. Mousa over 7 years@Unbreakable then return Json("Hello WOrld", JsonRequestBehavior.AllowGet);
-
Ramy M. Mousa over 7 years@Unbreakable check my answer, I Edited it.
-
Unbreakable over 7 yearsWhat about the ajax call. Do I need to change something in ajax call too. Can you see my ajax code..
-
Ramy M. Mousa over 7 years@Unbreakable I edited my answer, You don't really need to change anything in you ajax call.
-
Neo about 5 yearsbut id is going as url paramter visible in url , if i dont want that then?