1

我使用AJAX & WCF并且我想发送一个数组KeyValuePair

服务中的数组(即在特定类中):

[DataMember]
public KeyValuePair<string, CustomDay>[] WorkDays { get; set; }

CustomDay.cs:

[DataContract]
public class CustomDay
{
    [DataMember]
    public CustomTime Start { get; set; }
    [DataMember]
    public CustomTime End { get; set; }
}

自定义时间.cs:

[DataContract]
public class CustomTime
{
    [DataMember]
    public int Hour { get; set; }
    [DataMember]
    public int Minute { get; set; }
}

JavaScript我通过以下函数创建数组:

function CreateWorkDaysDictonary() {

    var workdaysdic = new Object();

    if ($('#checkbox-sunday').is(':checked')) {
        var starttime = { Hour: $('#select-from-hour-sunday').val(),
            Minute: $('#select-from-minute-sunday').val()
        };
        var endtime = { Hour: $('#select-until-hour-sunday').val(),
            Minute: $('#select-until-minute-sunday').val()
        };
        workdaysdic["Sunday"] = { Start: starttime, End: endtime };
    }

    if ($('#checkbox-monday').is(':checked')) {
        var starttime = { Hour: $('#select-from-hour-monday').val(),
            Minute: $('#select-from-minute-monday').val()
        };
        var endtime = { Hour: $('#select-until-hour-monday').val(),
            Minute: $('#select-until-minute-monday').val()
        };
        workdaysdic["Monday"] = { Start: starttime, End: endtime };
    }

    //and so on...

    return workdaysdic;
}

数组创建于JS

在此处输入图像描述

但是WCF数组到达时没有数据

在此处输入图像描述

我不明白为什么会发生这种情况,我会很高兴有人可以帮助我。

我将对象发送到 WCF JSON

在此处输入图像描述

当我尝试创建对象时喜欢这样:

function CreateWorkDaysDictonary() {

    var workdaysdic = new Array();

    if ($('#checkbox-sunday').is(':checked')) {
    var starttime = { Hour: $('#select-from-hour-sunday').val(),
        Minute: $('#select-from-minute-sunday').val()
    };
    var endtime = { Hour: $('#select-until-hour-sunday').val(),
        Minute: $('#select-until-minute-sunday').val()
    };
    workdaysdic[workdaysdic.length] = { "Sunday": { Start: starttime, End: endtime}};
}

if ($('#checkbox-monday').is(':checked')) {
    var starttime = { Hour: $('#select-from-hour-monday').val(),
        Minute: $('#select-from-minute-monday').val()
    };
    var endtime = { Hour: $('#select-until-hour-monday').val(),
        Minute: $('#select-until-minute-monday').val()
    };
    workdaysdic[workdaysdic.length] = { "Monday": { Start: starttime, End: endtime} };
}

        //and so on...

        return workdaysdic;
    }

我得到错误:the server responded with a status of 400 (Bad Request)

4

1 回答 1

1

最后我设法通过以下方式解决了这个问题:

我创建了一个对象列表(WCF),而不是数组:

[DataMember]
public List<DataItem> WorkDays { get; set; }

数据项.cs:

[DataContract]
public class DataItem
{
    [DataMember]
    public string Key;

    [DataMember]
    public CustomDay Value;
}

我发送了这样的列表(JS):

function CreateWorkDaysDictonary() {

    var workdaysdic = new Array();

    if ($('#checkbox-sunday').is(':checked')) {
        var starttime = { Hour: $('#select-from-hour-sunday').val(),
            Minute: $('#select-from-minute-sunday').val()
        };
        var endtime = { Hour: $('#select-until-hour-sunday').val(),
            Minute: $('#select-until-minute-sunday').val()
        };
        workdaysdic.push({ Key: "Sunday", Value: { Start: starttime, End: endtime} });
    }

    if ($('#checkbox-monday').is(':checked')) {
        var starttime = { Hour: $('#select-from-hour-monday').val(),
            Minute: $('#select-from-minute-monday').val()
        };
        var endtime = { Hour: $('#select-until-hour-monday').val(),
            Minute: $('#select-until-minute-monday').val()
        };
        workdaysdic.push({ Key: "Monday", Value: { Start: starttime, End: endtime} });
    }

    //and so on...

    return workdaysdic;
}

它有效!:)

我将在这里提到它适用于字典,但我没有使用它,因为我必须对 xml 文件进行序列化。

于 2014-04-06T06:39:15.747 回答