locked
List of VB.NET objects in JavaScript RRS feed

  • Question

  • User-450085702 posted

    Hi, 

    I have a simple form that calls a web service to save the data entered by the user. I am trying to save all the values in an object, and send that to the web service during the JQuery call. So I have all the values in Javascript, I want to convert them to be compatible with VB.Net class.  

    For example, the web service receives a parameter of type class "Car", the javascript should be able to send an object that is compatible to  VB.net class "Car". I am able to do this if the class "Car" has simple properties like String, Integer, and List(Of String). 

    VB.NET Class

    Class Car {

    model string,

    milage int,

    dimensions List(Of String)

    }

    Javascript code: 

    var objdimenstions = {

    width: "100",

    height: "200"

    }

    var objCar = {

    model : "BMW",

    milage: "20"

    dimensions : objdimenstions

    };

    The problem comes when the class has a property that is type another class.  To Elaborate, 

    VB.NET Classes: 

    Class HighEndCars {

    priceRange real;

    cars List(Of Car);

    }

    So, the question is, how to create list of objects in JavaScript, that can be sent as parameter to VB.NET ?  

    Regards,

    Karthik 

    Tuesday, September 6, 2016 8:24 PM

Answers

  • User-450085702 posted

    Hi, 

    Finally, I am able to serialize it to List(Of <ClassName>). It was a simple mistake: a constructor needed in <ClassName>. Thanks for your time and concern to reply. 

    Regards,

    Karthik

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, September 8, 2016 8:59 PM

All replies

  • User-474980206 posted

    you use arrays in JSON to model .net lists. I don't use VB, but the VB class has dimension as List<string>,  not an object.

    Wednesday, September 7, 2016 1:09 AM
  • User-450085702 posted

    Hi Bruce,

    I am able to construct JSON array, but unable to serialize them to List(Of <ClassName>) when passed as a parameter to Webservice. I am copying the actual code below. 

    JSON Array :

    var data = {
                                           "PersonID": personIDVal,
                                            "QuestionAndResponse": [{ "QuestionID": declaration1ID, "QuestionResponse": Response1 },
                                                                                      { "QuestionID": declaration2ID, "QuestionResponse": Response2 },
                                                                                      { "QuestionID": declaration3ID, "QuestionResponse": Response3 },
                                                                                      { "QuestionID": declaration4ID, "QuestionResponse": Response4 },
                                                                                      { "QuestionID": declaration5ID, "QuestionResponse": Response5 }
                                                                                     ]
                                         }

    Webservice Method:

    <WebMethod(EnableSession:=True)> _
    <ScriptMethod(ResponseFormat:=ResponseFormat.Json)> _
    Public Function SaveQuestionAndResponse(ByVal data As Responses) As String

    Try

        ......................

    Catch ex As Exception

       .....................

    EndTry

    End Function

    Classes:

    Class Responses {

    Private _PersonID As String;

    Private _QuestionAndResponse As List(Of SingleResponse)

    <<--Not copying the get and set of above two, but they exist-->>

    }

    Class SingleResponse {

    Private _QuestionID Integer;

    Privae _QuestionResponse String;

    <<--Not copying the get and set of above two, but they exist-->>

    }

    The control reaches the class Responses and receives nothing inside the list property _QuestionAndResponse. Please suggest me a way to achieve this. 

    Regards,

    Karthik

    Thursday, September 8, 2016 2:52 PM
  • User-450085702 posted

    Hi, 

    Finally, I am able to serialize it to List(Of <ClassName>). It was a simple mistake: a constructor needed in <ClassName>. Thanks for your time and concern to reply. 

    Regards,

    Karthik

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, September 8, 2016 8:59 PM