简体   繁体   中英

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

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)

        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM