Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/asp.net-mvc-3/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net mvc 3 从控制器返回不同的javascript对象_Asp.net Mvc 3_Jquery - Fatal编程技术网

Asp.net mvc 3 从控制器返回不同的javascript对象

Asp.net mvc 3 从控制器返回不同的javascript对象,asp.net-mvc-3,jquery,Asp.net Mvc 3,Jquery,我的控制器操作: [HttpPost] public ActionResult AddPointAndCopyOtherSongToPlaylist(int id) { if (CheckIfAddPointToSelf(User.Identity.Name, id)) { var song = repository.GetSong(id); foreach (var item in song

我的控制器操作:

 [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;

        }
    }
根据不同的场景,我希望返回一个javascript,并以某种方式通知客户端返回的内容,并根据结果在ajax调用的成功部分执行以下操作:

  $.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") }
        });
    });

我尝试了许多不同的变体,但我想我需要一些类似于data.property1的内容返回并在客户端中检查该属性是否存在或类似的内容。。请帮助

您需要返回对象中的状态代码

return Json( new { data1 = "Some Other Data", status = 1} );
然后在成功处理程序中检查data.status

if (data.status === 1) {
     alert(data.data1);
} 

所以我可以做一些类似于status=1或status=2的事情,然后对status进行chceck,它等于多少?