Search code examples
jqueryasp.netajaxweb-serviceswebmethod

How do i pass parameters from JQuery to ASP.NET webmethod?


I have written this jQuery ajax method below which calls a webmethod. The call happens fine except the parameter which is a User object has empty fields. I can see the values in firebug when i debug but they do not get through to the User object parameter in the webmethod

There are two values i am trying to pass from the my jQuery method to the Webmethod which are "UserID" (Guid) and "About" (string) which are both properties of the User class, but on the service end, the User object is just empty. Please help me identify whatever i am missing. Thank you ... See code below.

JQuery

function updatePersonalProfile(userId) {
var user = {};
user.UserID = userId;
var updatedPersonalProfile = $(".txtPersonalProfile").html();
user.About = updatedPersonalProfile;
$.ajax({
    type: "POST",
    url: "PresentationService.asmx/updateUserPersonalProfile",
    dataType: "json",
    data: "{user:" + JSON.stringify(user) + "}",
    contentType: "application/json; charset=utf-8",
    success: function(response) {
    },
    error: function(response) {
        alert(response.d);
    }
});
}

Webmethod

    [WebMethod]
    [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
    public void UpdateUserPersonalProfile(User user)
    {
        if (null == portfolioService)
        {
            portfolioService = new PortfolioService();
        }
        portfolioService.updateUserPersonalProfile(user);
    }

Solution

  • So I figured what i was doing wrong, both fields have private setter in the Domain object. therefore jQuery cant set the properties.

    thanks all