Passing A List Of Objects Into An MVC Controller Method Using jQuery Ajax

前端 未结 13 1728
逝去的感伤
逝去的感伤 2020-11-22 14:22

I\'m trying to pass an array of objects into an MVC controller method using jQuery\'s ajax() function. When I get into the PassThing() C# controller method, the argument \"t

13条回答
  •  醉梦人生
    2020-11-22 14:35

    Couldn't you just do this?

    var things = [
        { id: 1, color: 'yellow' },
        { id: 2, color: 'blue' },
        { id: 3, color: 'red' }
    ];
    $.post('@Url.Action("PassThings")', { things: things },
       function () {
            $('#result').html('"PassThings()" successfully called.');
       });
    

    ...and mark your action with

    [HttpPost]
    public void PassThings(IEnumerable things)
    {
        // do stuff with things here...
    }
    

提交回复
热议问题