2

My Ajax is not hitting my controller action. What could be the reason?

My Controller Action

 public ActionResult Save_Data(string [][] rosterArray, int Pricelist_Id, int Group_Id)

My Ajax

$.ajax({
    url: '@Url.Action("Save_Data", "PriceListMaster")',
    type: 'Post',
    async: false,
    contentType: 'application/json',
    dataType: "json",
    data: {  "rosterArray": rosterArray, "Pricelist_Id": "2", "Group_Id": $("#Group_Id").val() },
    //data: JSON.stringify({ "Item_Code": Item_Code, "IPD_Price": IPD_Price, "OPD_Price": OPD_Price, "EMS_Price": EMS_Price }),
    success: function (data) {
        debugger
        if (data.success)
        {
            alert('Data Saved Successfully.');
            location.reload();
        } else {
            alert(data.error_msg);
        }
    }
}
2
  • What's the value of rosterArray? Commented Mar 7, 2016 at 6:16
  • stringify the array data: { "rosterArray": JSON.stringify(rosterArray), ....... and at serverside read it as atring type from which you can phrase it back to array Commented Mar 7, 2016 at 9:14

2 Answers 2

4

If you are using type: 'Post' in AJAX, that means you need to add a Request type in Controller.

Try using this in controller,

[HttpPost]
public ActionResult Save_Data(string [][] rosterArray, int Pricelist_Id, int Group_Id)

Use httpPost in the Method for define type of response.

Sign up to request clarification or add additional context in comments.

1 Comment

Yes this would be a great place to start. If you already have it, try removing all your parameters and see if you hit your action and then add one at the time. But I think the Smit answer would do it.
1

at client side

$.ajax({
    url: '@Url.Action("Save_Data", "PriceListMaster")',
    type: 'Post',
    async: false,
    contentType: 'application/json',
    dataType: "json",
    data: {  "rosterArray": JSON.stringify(rosterArray), "Pricelist_Id": "2", "Group_Id": $("#Group_Id").val() },
    //data: JSON.stringify({ "Item_Code": Item_Code, "IPD_Price": IPD_Price, "OPD_Price": OPD_Price, "EMS_Price": EMS_Price }),
    success: function (data) {
        debugger
        if (data.success)
        {
            alert('Data Saved Successfully.');
            location.reload();
        } else {
            alert(data.error_msg);
        }
    }
}

and your controller code can be like this:

[HttpPost]
public ActionResult Save_Data(string rosterArray, int Pricelist_Id, int Group_Id)
{
    string [][] convertedArray = JsonConvert.DeserializeObject<string [][]>(rosterArray);
}

Hope this works Note: But you need to incude using Newtonsoft.Json;

1 Comment

You should add the [HttpPost] attribute to make it clear that the controller method needs to accept a post (since the client code is making a post).

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.