Alan Budzinski
Alan Budzinski

Reputation: 809

return javascript from asp.net action

I'm working on a site where users can add points to other user, however once a user added a point to other particular user he cannot add more points to the same user. So user1 can only give one point to user2.

On the server I check if there already exists a point with the same username. Everything works nicely so what I want to do now is to inform a user with some kind of message that he cannot add more points when he tries to do so since he already did add a point before. I think what i need is to return javascript from the server that will display the message. But maybe there is some other solution to this. Here is my action in controller:

[HttpPost]
    public ActionResult AddPointAndCopyCurrentFavToPlaylist(int id)
    {
        if (CheckIfPointExists(User.Identity.Name, id))
        {
        var originalSong = repository.GetCurrentFav(id);

        var newSong = new Song();
        newSong.UserName = User.Identity.Name;
        newSong.Title = originalSong.Title;
        newSong.YoutubeLink = originalSong.YoutubeLink;
        newSong.GenreId = 38;
        newSong.Date = DateTime.Now;
        repository.AddSong(newSong);

        var point = new Point();
        point.UsernameGotPoint = originalSong.UserName;
        point.UsernameGavePoint = User.Identity.Name;
        point.Date = DateTime.Now;
        point.Score = 1;
        point.CurrentFavId = id;
        repository.AddPoint(point);

        repository.Save();
        return RedirectToAction("Index");
        }
        else return JavaScript(???);
    }

here is my jquery Ajax:

  $(".btnAddOtherSongPoints").click(function () {
        var songId = $(this).attr("name");

        $.ajax({
            beforeSend: function () { ShowAjaxLoader(); },
            url: "/Home/AddPointAndCopyOtherSongToPlaylist/",
            type: "POST",
            data: { id: songId },
            success: function () { HideAjaxLoader(), ShowMsg("Song Added Successfully") },
            error: function () { HideAjaxLoader(), ShowMsg("Song could not be added, please try again") }
        });
    });

based on whether he was not allowed to give as point in the success part of my ajax request I want to display a different message so i'm guessing I would need an if statement in there to check whether the data object is not null (that means the server returned something, in this case a javascript function or something like that)

Upvotes: 0

Views: 102

Answers (2)

Ed B
Ed B

Reputation: 6054

Well, when you return JavaScript from the controller, you are mixing 'View' functionality with 'Controller' functionality.

You could do this, but you're going against convention & it may make it harder for the next developer on it to maintain.

Your Controller should only return data. You can check the data in your JavaScript for a success flag & display a message from there.

Edit:

In your controller:

    bool isSuccess = true;
   //process here
    return Json(new {Result = isSuccess}, JsonRequestBehavior.AllowGet);

In your JavaScript you check to see if Result == true.

Replace the success function in your $.ajax with something like this:

success: function (result) { 
   if (!result)
     { alert('Oh nooes'); }
},

Upvotes: 1

Cedric
Cedric

Reputation: 684

Why did you not return boolean result on your reponse (Whith JSON Method why not) to indicate if the point is added or not. And on client side, verify this value and show message if it's false. Like this :

var result = true;
...
return JSON(new { result } );

Upvotes: 0

Related Questions