Sherin1589
Sherin1589

Reputation: 15

MVC controller method is not being called from ajax

MVC controller method is not being called from the ajax i have declared. PFB the code snippet C# Controller:

public ActionResult Checkfunction(string ReqID, string AssociateId, string AssetId)
{
      MyDetails obj = new MyDetails();
      List<string> Lst = new List<string>();
      Lst = obj.Check(AssociateId, AssetId, ReqID);
      return this.Json(Lst, "text/json");
}

Javascript code (ajax call): refering details controller, and webmethod Checkfunction

$.ajax({
    type: 'GET',
    cache: false,
    url: '@Url.Action("Details/Checkfunction")',
    data: { 'ReqID': RequestId, 'AssociateId': AssociateID, 'AssetId': Host_Name },
    contentType: "application/json",
    success: function (data) {
        debugger;
        if (data.length > 0) {

                ViewModel.REQUESTID() = data[0];
                ViewModel.FLAG() = '1';
        }
        else {
            debugger;
            ViewModel.FLAG() = '0';
            ViewModel.REQUESTID() = '';
        }

        if (ViewModel.REQUESTID() != '' || ViewModel.REQUESTID() != null) {
            debugger;
            ViewModel.REQID() = RequestId;
        }
    },

    error: function (error) {
        alert("error");
    }
});

Upvotes: 0

Views: 127

Answers (3)

Leo Nix
Leo Nix

Reputation: 2105

  1. Build the url correctly:

    $.ajax({ type: 'POST', cache: false, url: '@Url.AbsoluteAction("PhoenixInbox", "Checkfunction")',

  2. Make sure you allow get on Get action: JsonRequestBehavior.AllowGet

    public ActionResult Checkfunction(string ReqID, string AssociateId, string AssetId) { MyDetails obj = new MyDetails(); List Lst = new List(); Lst = objMyAssetsDetails.Check(AssociateId, AssetId, ReqID); return this.Json(Lst, "text/json", JsonRequestBehavior.AllowGet ); }

Upvotes: 0

ChaosPattern
ChaosPattern

Reputation: 124

nowadays it's better to use promises, and if you're going to return json, it's better return JsonResult instead of ActionResult

http://davidwalsh.name/write-javascript-promises

Upvotes: 0

Hardik Gondalia
Hardik Gondalia

Reputation: 3717

Try this:

$.ajax({
type: 'POST',
cache: false,
url: '/PhoenixInbox/Checkfunction',
data: { 'ReqID': RequestId, 'AssociateId': AssociateID, 'AssetId': Host_Name },
contentType: "application/json",
success: function (data) {
    debugger;
    if (data.length > 0) {

            ViewModel.REQUESTID() = data[0];
            ViewModel.FLAG() = '1';
    }
    else {
        debugger;
        ViewModel.FLAG() = '0';
        ViewModel.REQUESTID() = '';
    }

    if (ViewModel.REQUESTID() != '' || ViewModel.REQUESTID() != null) {
        debugger;
        ViewModel.REQID() = RequestId;
    }
},

error: function (error) {
    alert(JSON.stringify(error));
}
});

Controller:

[Httppost]
public ActionResult Checkfunction(string ReqID, string AssociateId, string AssetId)
{
      MyDetails obj = new MyDetails();
      List<string> Lst = new List<string>();
      Lst = objMyAssetsDetails.Check(AssociateId, AssetId, ReqID);
      return this.Json(Lst, "text/json");
}

Upvotes: 1

Related Questions