gpt4 book ai didi

javascript - 将一组 Javascript 类传递给 MVC Controller ?

转载 作者:行者123 更新时间:2023-11-29 10:47:34 25 4
gpt4 key购买 nike

我正在尝试将一系列服务传递给我的 Controller 。我已经尝试了很多不同的方法来让它工作,在进入 Controller 之前序列化数据,序列化每个服务,唯一似乎有效的是将 Controller 参数更改为字符串并序列化数组,然后使用 JsonConvert,但我'我宁愿不那样做。

使用指定的代码,我在列表中获得了正确数量的项目,但它们都包含一个带有空行会的服务 ID,并且服务提供商 ID 为空。

有什么想法吗?

Javascript

function ServiceItem() {    this.ServiceProviderID = 'all';    this.ServiceID = '';}var selecteditems= (function () {    var services = new Array();    return {       all: function() {            return services;        },       add: function(service) {           services.push(service);       }    };})();var reserved = [];$.each(selecteditems.all(), function(index, item){   reserved.push({ ServiceID: item.ServiceID, ServiceProviderID: item.ServiceProviderID});});getData('Controller/GetMethod', { items: reserved }, function(result) {});var getData = function (actionurl, da, done) {       $.ajax({           type: "GET",           url: actionurl,           data: da,           dataType: "json",           async: true,           success: function (d) {               if (typeof (done) == 'function') {                   var str = JSON.stringify(d);                   done(JSON.parse(str));               }           }       });};

Controller

public JsonResult GetMethod(List<CustomObject> items){}

自定义对象

public class CustomObject{   public Guid ServiceID {get;set;}   public Guid? ServiceProviderID {get;set;}}

最佳答案

设置内容类型并使用 POST 而不是 GET(因为它是复杂类型对象的列表)。也用 HttpPost 属性标记您的操作。

看看这是否有效:-

 $.ajax({
type: "POST",
url: actionurl,
data: JSON.stringify(da),
dataType: "json",
contentType: 'application/json',
async: true,
success: function (d) {
if (typeof (done) == 'function') {
var str = JSON.stringify(d);
done(JSON.parse(str));
}
}
});

关于javascript - 将一组 Javascript 类传递给 MVC Controller ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16824773/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com