-1

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.

4

1 回答 1

3

当您传递 ByVal 时,您传递了变量的副本。当您传递 ByRef(通过引用)时,您传递了对象引用(简单地说,您传递了确切的原始变量)。

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
于 2013-10-09T15:27:18.490 回答