0

my controller action:

 [HttpPost]
    public ActionResult AddPointAndCopyOtherSongToPlaylist(int id)
    {   
        if (CheckIfAddPointToSelf(User.Identity.Name, id))
        {
            var song = repository.GetSong(id);
            foreach (var item in song.Points)
            {
                if (User.Identity.Name == item.UsernameGavePoint)
                {
                   var data1 = 1;


                    return Json(new {data1}, JsonRequestBehavior.AllowGet);
                }

            }
            var originalSong = repository.GetSong(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.OtherSongId = id;
            repository.AddPoint(point);
            repository.Save();

           int data = 2;
            //process here
            return Json(new { data }, JsonRequestBehavior.AllowGet);
        }
        else
        {
            return null;

        }
    }

based on different scenarios I want to return a javascript and somehow notify the client of what was returned and based in the result do something in the success part of my ajax call:

  $.ajax({
            beforeSend: function () { ShowAjaxLoader(); },
            url: "/Home/AddPointAndCopyOtherSongToPlaylist/",
            type: "POST",
            data: { id: songId },
            success: function (data,one) {

                if (data && !one) {
                    HideAjaxLoader(), ShowMsg("Song Added Successfully");
                }
                else if(!data) {
                    HideAjaxLoader(), ShowMsg("you cannot add your own songs");
                }
                else if (data && one) {
                    HideAjaxLoader(), ShowMsg("You cannot add the same song twice");
                }
            },
            error: function () { HideAjaxLoader(), ShowMsg("Song could not be added, please try again") }
        });
    });

I tried many different variations but I think i need something like data.property1 returned and in the client to check if that property exists or soemthing like that.. please help

1 Answer 1

1

You need to return your status code within the object.

return Json( new { data1 = "Some Other Data", status = 1} );

Then in your success handler check data.status.

if (data.status === 1) {
     alert(data.data1);
} 
Sign up to request clarification or add additional context in comments.

1 Comment

so I can do something like status = 1 or status = 2 and then chceck for the status what number it equals?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.