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

5
  • 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. Commented Sep 30, 2015 at 14:01
  • tried, it but still no success, i edit my question Commented Sep 30, 2015 at 14:05
  • 2
    possible duplicate of Using $.ajax or $.post to call MVC 5 Controller method Commented Sep 30, 2015 at 14:16
  • If you use an [Ajax ActionLink][1], you can pass myDataArray in the RouteValues. [1]: stackoverflow.com/questions/5586327/how-to-use-ajax-actionlink Commented Sep 30, 2015 at 14:19
  • dates isn't an array of strings. You have an array of string arrays based on your JSON. Commented Sep 30, 2015 at 14:21

1 Answer 1

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)

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

1 Comment

ah ok , i missunderstood what you said, i got it now , thanks

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.