路过的Javascript类的数组到MVC控制器? [英] Passing an array of Javascript classes to a MVC controller?

查看:168
本文介绍了路过的Javascript类的数组到MVC控制器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图一系列的服务传递给我的控制器。 我已经试过了一堆不同的方式来得到它的工作,才去到控制器,每个序列化的服务,只似乎工作是改变控制器参数字符串和序列化数组,然后使用JsonConvert事情序列化的数据,但我宁可不这样做。

通过指定code,我得到列表中的项目的正确的号码,但都包含服务ID和一个空行会,和服务供应商id为null。

任何想法?

Javascript的


功能ServiceItem(){
    this.ServiceProviderID ='所有';
    this.ServiceID ='';
}

VAR selecteditems =(函数(){

    VAR的服务=新的Array();

    返回 {
       所有:函数(){
           返回服务;
       },
       地址:功能(服务){
           services.push(服务);
       }
    };

})();

VAR保留= [];
$每个(selecteditems.all()函数(指数,项目){
   reserved.push({服务ID:item.ServiceID,ServiceProviderID:item.ServiceProviderID});
});

的getData(控制器/ GetMethod的',{项:保留},功能(结果){
});

VAR的getData =功能(actionurl,DA,做){
       $阿贾克斯({
           键入:GET,
           网址:actionurl,
           数据:DA,
           数据类型:JSON,
           异步:真正的,
           成功:函数(D){
               如果(typeof运算(完成)=='功能'){
                   VAR海峡= JSON.stringify(D);
                   做(JSON.parse(STR));
               }
           }
       });
};


控制器

公共JsonResult GetMethod的(名单< CustomObject>项目)
{
}

自定义对象

公共类CustomObject
{
   公众的Guid服务ID {获取;集;}
   公众的Guid? ServiceProviderID {获取;集;}
}

解决方案

设置Content-Type和使用GET的POST而不是(因为它是复杂类型​​对象的列表)。标志着你的动作与HttpPost属性了。

看看这个工程: -

  $。阿贾克斯({
           键入:POST,
           网址:actionurl,
           数据:JSON.stringify(DA),
           数据类型:JSON,
           的contentType:应用/ JSON的,
           异步:真正的,
           成功:函数(D){
               如果(typeof运算(完成)=='功能'){
                   VAR海峡= JSON.stringify(D);
                   做(JSON.parse(STR));
               }
           }
       });
 

I am trying to pass an array of services to my controller. I've tried a bunch of different ways to get it work, serializing the data before going to controller, serializing each service, only thing that seems to work is changing the controller parameter to string and serializing array, then using JsonConvert, but I'd rather not do that.

With the specified code, I am getting the correct number of items in the List, but they all contain a service id with an empty guild, and service provider id is null.

Any ideas?

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)
{
}

Custom Object

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

解决方案

Set the content-type and use POST instead of GET (as it is a list of complex type objects). mark your action with HttpPost attribute too.

See if this works:-

 $.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类的数组到MVC控制器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆