How to make a AJAX GET call in ASP.NET MVC application

31,328

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);
        }
Share:
31,328
Unbreakable
Author by

Unbreakable

The fool didn't know it was impossible, so he did it!

Updated on July 09, 2022

Comments

  • Unbreakable
    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 an Ajax GET call to one of the function that I have written in the HomeController and action name is Test. I just want to hit the breakpoint in that Test Action of Homecontroller and return something back to the Success 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 for Test. But I actually do not want that. I just want the Test Action to return me one data. And I will display that data in my Already opened Index.csthml file.

    Error: No error but I am not able to hit the breakpoint in Test Action of controller. Kindly note that Success of AJAX is hitting but I do not see any data. But I am sure it did not hit the test Action breakpoint.

  • Unbreakable
    Unbreakable over 7 years
    I do not understand your answer. I just want to return one string. may be "hello world".
  • Unbreakable
    Unbreakable over 7 years
    What is this int? id and myListQuery and all.
  • Ramy M. Mousa
    Ramy M. Mousa over 7 years
    @Unbreakable then return Json("Hello WOrld", JsonRequestBehavior.AllowGet);
  • Ramy M. Mousa
    Ramy M. Mousa over 7 years
    @Unbreakable check my answer, I Edited it.
  • Unbreakable
    Unbreakable over 7 years
    What about the ajax call. Do I need to change something in ajax call too. Can you see my ajax code..
  • Ramy M. Mousa
    Ramy M. Mousa over 7 years
    @Unbreakable I edited my answer, You don't really need to change anything in you ajax call.
  • Neo
    Neo about 5 years
    but id is going as url paramter visible in url , if i dont want that then?