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,它等于多少?