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

前端 未结 13 1596
逝去的感伤
逝去的感伤 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:45

    I have perfect answer for all this : I tried so many solution not able to get finally myself able to manage , please find detail answer below:

           $.ajax({
                traditional: true,
                url: "/Conroller/MethodTest",
                type: "POST",
                contentType: "application/json; charset=utf-8",
                data:JSON.stringify( 
                   [
                    { id: 1, color: 'yellow' },
                    { id: 2, color: 'blue' },
                    { id: 3, color: 'red' }
                    ]),
                success: function (data) {
                    $scope.DisplayError(data.requestStatus);
                }
            });
    

    Controler

    public class Thing
    {
        public int id { get; set; }
        public string color { get; set; }
    }
    
    public JsonResult MethodTest(IEnumerable datav)
        {
       //now  datav is having all your values
      }
    

提交回复
热议问题