Thought
Thought

Reputation: 5846

How to pass data from ajax javascript to MVC controller?

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

Upvotes: 0

Views: 1896

Answers (1)

Gaurav Jain
Gaurav Jain

Reputation: 444

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)

        }

Upvotes: 3

Related Questions