【问题标题】:Iterating through a Data Transfer Object inside the Web Method Call遍历 Web 方法调用中的数据传输对象
【发布时间】:2012-02-02 19:42:59
【问题描述】:

我正在使用 JQuery 将 DTO 发送到 Web 服务,但我需要知道在 Web 服务调用中收到该对象时如何迭代该对象。这是代码

JQUERY

$(divButtonText).click(function() {

            var QuestionItems = {};
            var foundEmptyFields = false;
            var count = 1;
            $(".diverror").hide();

            $(".cmsquestions").each(function() {

                var question = $($(this).find(".cmstextbox input")).val();
                var radioValue = $($($('input[name="responsefields' + count + '"]:checked')).next()).text();

                if (question.length > 0) {

                    var questionnumber = $(".cmspagenumber").length;

                    if (questionnumber == 0) {

                        questionnumber = 1;
                    }

                    var QuestionItem = {};

                    QuestionItem.TitleID = data.d;
                    QuestionItem.Number = questionnumber;
                    QuestionItem.Question = question;
                    QuestionItem.FieldType = radioValue;

                    QuestionItems[count - 1] = QuestionItem;
                }
                else {
                    foundEmptyFields = true;
                    var error = $(this).find(".diverror");
                    $(error).show();
                }

                count += 1;

            });

            if (!foundEmptyFields) {



                var DTO = { 'QuestionItems': QuestionItems };
                var param = JSON.stringify(DTO);
                Ajax_WebService(param, 'AddQuestion', AddQuestionServerResponse);

            }

        });

网络方法

 <WebMethod()> _
  Public Function AddQuestion(ByVal QuestionItems As Object) As Integer

    For i As Integer = 0 To QuestionItems.Count - 1



    Next


End Function

编辑

WEB服务调用

function Ajax_WebService(param, method, callback) {

$.ajax({
    type: "POST",
    url: "CMSAdmin.asmx/" + method,
    data: param,
    dataType: "json",
    contentType: "application/json; charset=utf-8",
    success: function(data) {
        if (callback) {
            callback.call(null, data);
        }

    }
});

}

编辑

好的,我已经把 WebMethod 改成了这个

 <WebMethod()> _
 Public Function AddQuestion(ByVal QuestionItems As List(Of QuestionItem)) As Integer




End Function

我现在可以遍历列表项,但我的 QuestionItem 类正在获取空值和空值,QuestionItem 类在这里

Public Class QuestionItem


    Private _TitleID As Integer
    Private _Number As Integer
    Private _Question As String
    Private _FieldType As String

    Public ReadOnly Property TitleID() As Integer
        Get
            Return Me._TitleID
        End Get
    End Property

    Public ReadOnly Property Number() As Integer
        Get
            Return Me._Number
        End Get
    End Property

    Public ReadOnly Property Question() As String
        Get
            If Me._Question Is Nothing Then
                Return String.Empty
            End If
            Return Me._Question
        End Get
    End Property

    Public ReadOnly Property FieldType() As String
        Get
            If Me._FieldType Is Nothing Then
                Return String.Empty
            End If
            Return Me._FieldType
        End Get
    End Property
End Class

为什么 JSON 对象的属性返回 null 和空值,该类结构与 JSON 类结构相同?

【问题讨论】:

  • 代码中的 for 循环已经在迭代。
  • amit_g: 但我似乎无法从循环中获取值

标签: jquery asp.net .net web-services


【解决方案1】:

您可以使用new System.Web.Script.Serialization.JavaScriptSerializer().Deserialize 方法将传入的字符串转换为与您的QuestionItem 集合匹配的类型;然后循环。

【讨论】:

  • 但是传递给 web 服务的数据是一个对象,我可以反序列化这个对象吗?
  • 将 webmethod arg 更改为字符串,因为传入的类型是字符串(你正在做JSON.stringify(DTO);,这是正确的,顺便说一句)。 QuestionItems As Object 可以转换为字符串。
  • 我已尝试更改为字符串,但仍然没有运气,我尝试将对象转换为字符串,但我担心这不起作用
  • 有什么错误吗?或者你得到的字符串值是什么(至少粘贴一个 sn-p)。
  • 没有错误,只是将对象更改为字符串数据类型时没有调用 Web 服务
【解决方案2】:

这是使用 QuestionItem 类设置 JSON 请求值的 WebMethod 调用

<WebMethod()> _
 Public Function AddQuestion(ByVal QuestionItems() As QuestionItem) As Integer


    For Each item As QuestionItem In QuestionItems




    Next


End Function

Public Class QuestionItem


    Private _TitleID As Integer
    Private _Number As Integer
    Private _Question As String
    Private _FieldType As String

    Public Property TitleID() As Integer
        Get
            Return Me._TitleID
        End Get
        Set(ByVal value As Integer)
            Me._TitleID = value
        End Set
    End Property

    Public Property Number() As Integer
        Get
            Return Me._Number
        End Get
        Set(ByVal value As Integer)
            Me._Number = value
        End Set
    End Property

    Public Property Question() As String
        Get
            If Me._Question Is Nothing Then
                Return String.Empty
            End If
            Return Me._Question
        End Get
        Set(ByVal value As String)
            Me._Question = value
        End Set
    End Property

    Public Property FieldType() As String
        Get
            If Me._FieldType Is Nothing Then
                Return String.Empty
            End If
            Return Me._FieldType
        End Get
        Set(ByVal value As String)
            Me._FieldType = value
        End Set
    End Property
End Class

Jquery 和 Javascript 下面是如何格式化 JSON 请求客户端

$(divButton).click(function() {

            var QuestionItems = new Array();

            var foundEmptyFields = false;
            var count = 1;
            $(".diverror").hide();

            $(".cmsquestions").each(function() {

                var question = $($(this).find(".cmstextbox input")).val();
                var radioValue = $($($('input[name="responsefields' + count + '"]:checked')).next()).text();

                if (question.length > 0) {

                    var questionnumber = $(".cmspagenumber").length;

                    if (questionnumber == 0) {

                        questionnumber = 1;
                    }

                    var QuestionItem = {};

                    QuestionItem.TitleID = data.d;
                    QuestionItem.Number = questionnumber;
                    QuestionItem.Question = question;
                    QuestionItem.FieldType = radioValue;

                    QuestionItems.push(QuestionItem);
                }
                else {
                    foundEmptyFields = true;
                    var error = $(this).find(".diverror");
                    $(error).show();
                }

                count += 1;

            });

            if (!foundEmptyFields) {



                var DTO = "{ 'QuestionItems':" + JSON.stringify(QuestionItems) + "}";
                var param = DTO;
                Ajax_WebService(param, 'AddQuestion', AddQuestionServerResponse);

            }

        });


function Ajax_WebService(param, method, callback) {

$.ajax({
    type: "POST",
    url: "CMSAdmin.asmx/" + method,
    data: param,
    dataType: "json",
    contentType: "application/json; charset=utf-8",
    success: function(data) {
        if (callback) {
            callback.call(null, data);
        }

    }
});

}

【讨论】:

    猜你喜欢
    • 2012-04-15
    • 1970-01-01
    • 1970-01-01
    • 2017-09-14
    • 1970-01-01
    • 2021-02-13
    • 2012-10-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多