0

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");
    }
});
4
  • 2
    how come the Checkfunction is not in your ajax call? Commented Oct 8, 2015 at 13:01
  • 1
    your ajax url is pointed to PhoenixInbox/CheckMSDNRemap, but your method is Checkfunction? That would be a good starting point Commented Oct 8, 2015 at 13:01
  • 1
    What does GetUrl() do? Commented Oct 8, 2015 at 13:02
  • simply change the url properties to url: '/PhoenixInbox/Checkfunction' Commented Oct 8, 2015 at 13:05

3 Answers 3

1

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");
}
Sign up to request clarification or add additional context in comments.

1 Comment

'/PhoenixInbox/Checkfunction' could blow up in production if the app is not deployed in root directory. So the url should be built with 'Url.Action' at runtime.
0

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

2 Comments

While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes.
Yes, you're right, I just registered today, I have several things to learn ;)
0
  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 ); }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.