如何将对象列表传递到MVC4中的控制器方法

how to pass list of objects in to controller method in MVC4?

本文关键字:MVC4 控制器 方法 对象 列表      更新时间:2023-09-26

我有对象列表:

var things = [];
var obj = {
            ayah: line.ayah,
            surah: line.surah,
            verse: line.verse
          };
          things.push(obj); 
$.ajax({
         method: 'GET',
         url: "Gateway/Inbound_Request_Handler?action=1",
         data:things,
         success: function (Data) {
         var mera_obj = Data.key;
         document.getElementById("Param2").value = '(' + mera_obj.Response_Code + ' , ' + mera_obj.Response_Description + ')';
         },
         error: function () {
         alert("ERROR: can't connect to Server this time");
                   }
               });

和类别:

public class thing {
        public int surah { get; set; }
        public int ayah { get; set; }
        public string verse { get; set; }
    }

这里是控制器方法:

public class GatewayController : Controller
    {
        [HttpGet]
        public ActionResult Inbound_Request_Handler(List<thing> things)
        {...}
    }

但它仍然显示控制器方法中的列表为空。我不知道我怎么了?

您的ajax调用指定data

data:things更改为data: {things: things}以指定传入对象的名称,这样MVC就可以绑定到它。