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

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

    Modification from @veeresh i

     var data=[
    
                            { id: 1, color: 'yellow' },
                            { id: 2, color: 'blue' },
                            { id: 3, color: 'red' }
                            ]; //parameter
            var para={};
            para.datav=data;   //datav from View
    
    
            $.ajax({
                        traditional: true,
                        url: "/Conroller/MethodTest",
                        type: "POST",
                        contentType: "application/json; charset=utf-8",
                        data:para,
                        success: function (data) {
                            $scope.DisplayError(data.requestStatus);
                        }
                    });
    
    In MVC
    
    
    
    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
          }
    

提交回复
热议问题