Colin
Colin

Reputation: 22595

How do I consume a WebMethod with jQuery that has parameters other than strings?

I have this client-side:

$(document).ready(function() {
    var $checkboxes = $('[type=checkbox]');
    $checkboxes.click(function() {

        $checkbox = $(this);

        $.ajax({
            type: "POST",
            url: "Page.aspx/Method",
            data: "{id:'" + $checkbox.attr("id") + "'}",
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function(msg) {
                alert('success!');
            },
            error: function(xhr, status, error) {
                var err = eval("(" + xhr.responseText + ")");
                alert('An error occurred that prevented your change being saved: ' + err.Message);
            }
        });
    });
 });

And I have this server-side:

[WebMethod]
public static void Method(String id)
{
    Guid guidID = new Guid(classid));
    //...........
}

But I'd prefer to make the WebMethod signature strongly typed:

[WebMethod]
public static void Method(Guid id)
{
    //...........
}

Can you advise what is the best way to do this?

Upvotes: 1

Views: 450

Answers (2)

George Johnston
George Johnston

Reputation: 32278

You can have strongly typed parameter(s). If they're in the correct format, they'll be implicitly converted to the correct type upon hitting the method.

Upvotes: 2

DDiVita
DDiVita

Reputation: 4265

I think you can pass the Guid as a string into your method and it should convert it. check this out

Upvotes: 3

Related Questions