$ Ajax done function does not work in ASP.NET -MVC5 application

I am using the $ ajax jquery function on a partial razor to get another partial view along with strongly typed model data from the controller to the page -> display in a specific div. Now, if the data model data works there, but if there is no model data, I pass a json response so that I can check the appearance of the razor to avoid a null exception. My problem in the $ ajax method does not cause a plus json response, I do not know where I am wrong

Ajax function

$(document).ready(function () {

    /*Address*/
    $.ajax({
        url: '@Url.Action("DisplayStudentAddress")',
        type: "GET",
        cache: false
    }).done(function (data, textStatus, jqXHR) {

        alert(data.Response);

       $('#studentAddressDisplay').html(data);

    }).fail(function (jqXHR, textStatus, errorThrown) {

        alert(jqXHR +"    "+textStatus+"    "+errorThrown);
    });
});

ActionResult Method

 [HttpGet]
 [Authorize]
 public ActionResult DisplayStudentAddress()
    {
        int _studentEntityID = 0;

        _studentEntityID = _studentProfileServices.GetStudentIDByIdentityUserID(User.Identity.GetUserId());

        Address _studentAddressModel = new Address();

        _studentAddressModel = _studentProfileServices.GetStudentAddressByStudentID(_studentEntityID);


        if (_studentAddressModel != null)
        {
            return PartialView("DisplayStudentAddress_Partial", _studentAddressModel);
        }
        else
        {
             return Json(new { Response = "Provide Your Address Detail!" });
        }
    }
    #endregion

I have a debug check, json is called in the controller, but it warns about an error in ajax

+4
source share
3 answers

json, jQuery . json.

.

1.9, , JSON, JSON ( ); .

.always: -

$.ajax({
        url: '@Url.Action("DisplayStudentAddress")',
        type: "GET",
        cache: false
    }).always(function (data, textStatus, jqXHR) {

    alert(data.Response);

   $('#studentAddressDisplay').html(data);

}).fail(function (jqXHR, textStatus, errorThrown) {

        alert(jqXHR +"    "+textStatus+"    "+errorThrown);
    });

. done , , , - , .done . always ajax .

+3

, :

 return Json(new { Response = "Provide Your Address Detail!", RecordStatus ="Not Available" }, JsonRequestBehavior.AllowGet);
0

Change the code for this:

 if (data.itemCount == 0) {
                        $('#row-' + data.deleteId).fadeOut('slow');
                    } else {
                        $('#item-count-' + data.deleteId).text(data.itemCount);
                    }

                    $('#cart-total').text(data.cartTotal);
                    $('#update-message').text(data.message);
                    $('#cart-status').text(data.cartCount);
0
source

Source: https://habr.com/ru/post/1599246/


All Articles