I am sending a DTO to a web service using JQuery, but i need to know how to iterate through the object when received at the web service call. Here is the code
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
<WebMethod()> _
Public Function AddQuestion(ByVal QuestionItems As Object) As Integer
For i As Integer = 0 To QuestionItems.Count - 1
Next
End Function
EDIT
WEB Service Call
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);
}
}
});
}
EDIT
Ok i have changed the WebMethod to this
<WebMethod()> _
Public Function AddQuestion(ByVal QuestionItems As List(Of QuestionItem)) As Integer
End Function
I can now possibly iterate through the list items but my QuestionItem class is picking up values of nothing and empty, the QuestionItem class is here
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
Why is the properties returning null and empty values from the JSON Object, that class structure is the same as the JSON Class Structure?
This is the WebMethod call that uses QuestionItem Class to set the values from the JSON Request
The Jquery and Javascript is below of how to format out the JSON Request client side