0

In asp.net MVC project, I have this piece of javascript that calls a method in HomeController.

$.ajax({
            url: '@Url.Action("doMultiBook", "Home")',
            data: { "obj": myDataArray },
            type: 'GET',
            dateType: "json",
            cache:false,
            success: function (data) {



            },
            error: function (data) {
                console.log("ERROR " + data)

            }

        });

the myDataArray I'm passing is something like this:

[{"userId":11,"bench":366,"dates":[["2015-9-30","All Day"]]},{"userId":18,"bench":366,"dates":[["2015-9-30","All Day"]]},{"userId":25,"bench":366,"dates":[["2015-9-30","All Day"]]}]

EDIT then on my controller I have this,

    public void doMultiBook(multiBookObject[] obj)
    {
        Console.WriteLine(obj);

    }

and this is my multiBookObject class

public class multiBookObject
{
    public int userID { get; set; }
    public int bench { get; set; }
    public string[] dates { get; set; }
}

How do i call a controller method and pass some data to it?

edit my question after suggestion

Thought
  • 5,326
  • 7
  • 33
  • 69
  • 2
    Instead of `string var` make it an object that matches your JSON Definition, so basically an array of objects that has `userId`, `bench`, and an array of `date's. You're not passing a string, you're passing a JSON object so your C# needs an object that matches it. – dmeglio Sep 30 '15 at 14:01
  • tried, it but still no success, i edit my question – Thought Sep 30 '15 at 14:05
  • 2
    possible duplicate of [Using $.ajax or $.post to call MVC 5 Controller method](http://stackoverflow.com/questions/26976402/using-ajax-or-post-to-call-mvc-5-controller-method) – Carl Onager Sep 30 '15 at 14:16
  • If you use an [Ajax ActionLink][1], you can pass myDataArray in the RouteValues. [1]: http://stackoverflow.com/questions/5586327/how-to-use-ajax-actionlink – Teun Vos Sep 30 '15 at 14:19
  • `dates` isn't an array of strings. You have an array of string arrays based on your JSON. – dmeglio Sep 30 '15 at 14:21

1 Answers1

3

pass your data as

data: { "var": myDataArray }

        url: '@Url.Action("doMultiBook", "Home")',

        data: { "var": myDataArray },

        type: 'GET',
        dateType: "json",
        cache:false,
        success: function (data) {



        },
        error: function (data) {
            console.log("ERROR getBookedByUser " + data)

        }
Gaurav Jain
  • 444
  • 3
  • 13