0

I want to pass two string arrays from the view to controller using Ajax.But while passing it shows the error jquery-latest.min.js:4 POST http://localhost:5342/Purchase/ClearCart 500 (Internal Server Error) The Method is not passing to the controller.Tried with a break point.Here is how am passing string[]

 var items = $('.mids').map(function () {
        return $(this).val()
    }).get();
    var counts = $('.counts').map(function () {
        return $(this).val()
    }).get();



 if (BOLT.response.txnStatus == 'SUCCESS') {

                    alert("success");
                    $.ajax({
                        url: "/Purchase/ClearCart",
                        type: "POST",
                        contentType: "application/json",
                        //dataType: "text",
                        data: { mid: items, count: counts },
                        success: function (result) {
                            alert("success");
                        },

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

It returns "Failed" response and the above error.

Controller

[HttpPost]
        public void ClearCart(string[] mid,string[] count)
        {
        int uid=Convert.ToInt32(Session["uid"]);
        userService.ClearCart(uid, mid,count);

        }

Here is the values inside Mid and count:Values inside Mid and count

7
  • Can you show the exact value of data: { mid: items, count: counts }? We need to see that so we could make a model for it. Commented Nov 19, 2019 at 5:49
  • @Jerdine Sabio updated my question.Am just puting the values (mid,count) in two lists Commented Nov 19, 2019 at 6:14
  • Because you're not passing in json. Remove contentType: "application/json", Commented Nov 19, 2019 at 6:22
  • You are sending a single object from your jquery, in your controller get these parameters in a Map and then retrieve the values using keys mid and count Commented Nov 19, 2019 at 6:58
  • OH okay, to clarify those are just string arrays yes? Commented Nov 19, 2019 at 8:00

2 Answers 2

1

You need to bind the values to an object during POST.

  1. Create a Class to be bound
public class AjaxModel{
   public List<int> mid {get;set;}
   public List<int> count {get;set;}

   // Alternatively, try list string
   // public List<string> mid {get;set;}
   // public List<string> count {get;set;}
}
  1. Modify the ClearCart to include AjaxModel model on the parameters
[HttpPost]
public ActionResult ClearCart(AjaxModel model)
{
   return Content(model.mid.Count())

   // int uid=Convert.ToInt32(Session["uid"]);
   // userService.ClearCart(uid, model.mid, model.count);
}
  1. Modify ajax call to include contentType: application/json
$.ajax({
   url: "/Purchase/ClearCart",
   type: "POST",
   contentType: "application/json",
   data: { mid: items, count: counts },
   success: function (result) {
      alert("success");
   },
   error: function (e) {
      alert("Failed");
   }
});
Sign up to request clarification or add additional context in comments.

5 Comments

tried but its shows an error "has some invalid argument"
@codeseeker Can you include the data from mid and count? Console.log(items) Console.log(counts) I assumed they were list string, looks like they may not be
Sir,Added a screen Shot.Please have a look
@codeseeker I updated the class to use List<int>, I also changed the return type of the function to ActionResult for testing. Try it now
Got the answer sir,Thank You
1

I solved With @Jerdine Sabio 's answer with some adjustment.

 var clr = new Object();
    clr.mid = items;
    clr.count = counts;
    $.ajax({
        url: "/Purchase/ClearCart",
        type: "POST",
        contentType: "application/json",
        dataType: JSON,
        data: JSON.stringify(clr),
        success: function (result) {
            alert("success");
        },

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

in The Controller

 public class SampleModel
        {
            public string[] mid { get; set; }
            public string[] count { get; set; }
        }
        [HttpPost]
        public JsonResult ClearCart(SampleModel model)
        {

        int[] matids = Array.ConvertAll(model.mid, int.Parse);
        int[] mcounts = Array.ConvertAll(model.count, int.Parse);

          //rest of the code
        }

1 Comment

Mark yours as the answer so this could be closed. Good job.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.