Question

I'm using an ajax call to hit a database to load a drop down. Now, this is something we're going to be doing a lot where I work now that someone's figured out how to do it.

My supervisor has asked me to try and make my ajax call one that I can easily use for multiple pages and such without having to redo the code for each new page. My code:

Controller

Function Index(ByVal KovID As String) As JsonResult
    Dim db As New Database1Entities
    Dim record As New List(Of BodyStyle)
    record = (From b In db.BodyStyles Where b.KovID = KovID Select b).ToList
    Return Json(record, JsonRequestBehavior.AllowGet)
End Function

JQuery

function ajaxSuccess(record) {
    var drop2 = $('#Vehicle_BodyStyle_value');
    drop2.get(0).options.length = 0;
    drop2.get(0).options[0] = new Option("Please Select One", "-1");
    $.each(record, function (index, item) {
        drop2.get(0).options[drop2.get(0).options.length] = new Option(item.BodyStyle1, index);
    });
}

function ajaxError() {
    $('#Vehicle_BodyStyle_value').get(0).options.length = 0;
    $('#Vehicle_BodyStyle_value').get(0).options[0] = new Option("Error!", "-1");
    alert("Failed to load styles");
}

$('#Vehicle_KovId_value').change(function () {
    var kovID = $(this).val();
    var drop2 = $('#Vehicle_BodyStyle_value');
    if (kovID != null && kovID != '') {
        drop2.get(0).options.length = 0;
        drop2.get(0).options[0] = new Option('Please Select One', '-1');
        $.ajax({
            type: "GET",
            url: '/Ajax/Index',
            async: false,
            data: { KovID: kovID },
            contentType: "application/json; charset=utf-8",
            success: ajaxSuccess,
            error: ajaxError
        });
    }
});

Now, I don't know VB.NET. In fact, a coworker helped me with the controller, and he's not here to assist further in this matter.

My question, posed by my supervisor, is how can we make this work?

If the ByVal in the controller class is changed to a ByRef, can we pass something in for the data: line in the ajax call so that this code can be reused and not rewritten?

Any help or clarification will be greatly appreciated.

Was it helpful?

Solution

When you pass ByVal, you pass a copy of the, say, variable. When you pass ByRef (by reference), you pass the object reference (Simply, you pass the exact, original variable).

Dim exampleVariable As String = "hello"

MyMethodOne(exampleVariable) 'Call methodOne (We pass a COPY of exampleVariable )


Private Sub MyMethodOne(ByVal x As String)
      x = "Foo"
      Console.WriteLine(x) 'prints Foo
      Console.WriteLine(exampleVariable) 'prints hello
End Sub

MyMethodTwo(exampleVariable) 'Call method (we pass the REFERENCE to exampleVariable )
 Private Sub MyMethodTwo(ByRef x As String)
      x = "Foo"
      Console.WriteLine(x) 'prints Foo
      Console.WriteLine(exampleVariable) 'prints Foo
End Sub
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top