Getting complex object via jQuery / ajax in ASP.NET

Here is my situation: I have an object UserBadgein ASP.NET, it contains 3 fields, which is the object User, Badgeand boolean(isNotified) to check if the user has been notified of the receipt of the icon. I am having trouble sending certain UserBadgeof this WebMethod():

[WebMethod()]
    public static UserBadge Notify()
    {
        var db = new achievDb();

        foreach (var uB in db.UserBadges)
        {
            if (System.Web.HttpContext.Current.User.Identity.Name == uB.User.UserName)
            {
                if (!uB.isNotified)
                {
                    return uB;
                }
            }
        }
        return null;
    }

on mine $.ajax:

<script type="text/javascript">
            $(document).ready(function () {
                $.ajax({
                    type: "POST",
                    url: "../NotifCodeBehind.aspx/Notify",
                    data: "{}",
                    complete: function (result) {
                        if (result) {
                            $("#notify").jGrowl("You've unlocked a badge!", { header: 'Yay', close: function () {
                                $.ajax({
                                    type: "POST",
                                    url: "../NotifCodeBehind.aspx/Notified",
                                    data: "{}",
                                    success: function (ub) { DoCallback(JSON.stringify(ub)); },
                                    error: function () { DoCallback("NOPE!") }
                                });
                            }
                            })

                        };
                        function DoCallback(msg) {
                            alert(msg);
                        }
                    }
                })
            })
        </script>

and then back to another WebMethod(), which sets the value isNotified booleanto true after the notification is closed:

    [WebMethod()]
    public static void Notified(UserBadge ub)
    {
        var db = new achievDb();

            foreach (var userbadge in db.UserBadges)
            {
                if (userbadge.UserId == ub.UserId && userbadge.BadgeId == ub.UserId)
                {
                    userbadge.isNotified = true;
                    db.SaveChanges();
                }
        }
    }

: , ajax, ... 1,5 , . , , jQuery/Ajax/JSON.

, , !

EDIT: JavaScript , , , .

EDIT2: , WebMethods.

+5
3

, , ! WebMethods.

0

JSON. ajax, - XML, JSON . JSON, json .

class UserBadge
{
    User UserProperty { get; set; }
    Badge BadgeProperty { get; set; }
    bool IsNotified { get; set; }
}

class User
{
    string Username { get; set; }
}

json- javascript

{ 
  UserProperty: { Username: "some username" },
  BadgeProperty: { /**********/ },
  IsNotified: true 
}

, JSON , . , result.UserProperty.Username javascript . - ajax JSON .

: ScriptMethodAttribute WebMethod, JSON.

[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public static UserBadge Notify()
{
}
+5

-? (UserId, badgeId ..) ajax, , . .

var userId=4 // read from some hidden items or somewhere
var badgeid=3 // read from somewhere
$.ajax({
        type: "POST",
        url: "../NotifCodeBehind.aspx/Notify?uid="+userId+"&bid="+badgeId,
        data: "{}",
        complete: function (result) {

       // rest of the code

: , ASP.NET MVC

ASP.NET MVC, . jquery.

If your page has “LogOnMOdel” and you want to make this binding for the UserBadge object, you need to create another ViewModel that has 2 properties, one is LogonModel and the other is UserBadge. then submit this view model to your page.

+1
source

All Articles