Asp.net mvc 3 发送多个参数asp.net mvc jquery ajax
我得到服务器错误:当我试图用jquery ajax向控制器中的操作发送2个参数时,状态为500,不知道为什么 这是jquery代码:Asp.net mvc 3 发送多个参数asp.net mvc jquery ajax,asp.net-mvc-3,jquery,Asp.net Mvc 3,Jquery,我得到服务器错误:当我试图用jquery ajax向控制器中的操作发送2个参数时,状态为500,不知道为什么 这是jquery代码: $(".genreLinks").click(function () { var genreId = $(this).attr("name"); var songId = $("#hiddenRank").val(); $.ajax({ beforeSend: function () {
$(".genreLinks").click(function () {
var genreId = $(this).attr("name");
var songId = $("#hiddenRank").val();
$.ajax({
beforeSend: function () { ShowAjaxLoader(); },
url: "/Home/AddPointAndCopyTopTenFavToPlaylist/",
type: "POST",
data: { id: songId, genre: genreId },
success: function (data) {
if (data.status === 1) {
HideAjaxLoader(), ShowMsg("Song Added Successfully")
}
else if (data.status === 4) {
HideAjaxLoader(), ShowMsg("you cannot add your own songs");
}
else if (data.status === 3) {
HideAjaxLoader(), ShowMsg("Cannot add the song. The song was most likely modified or Deleted, we advise you to refresh the page");
}
else if (data.status === 2) {
HideAjaxLoader(), ShowMsg("You cannot add the same song twice");
}
},
error: function () { HideAjaxLoader(), ShowMsg("Song could not be added, please try again") }
});
});
从我的请求中接受两个参数的控制器操作代码:
[HttpPost]
public ActionResult AddPointAndCopyTopTenFavToPlaylist(int id, int genre)
{
var song = repository.GetTopTenFav(id);
if (song != null)
{
if (!(song.UserName.ToLower() == User.Identity.Name.ToLower()))
{
foreach (var item in song.Points)
{
if (User.Identity.Name.ToLower() == item.UsernameGavePoint.ToLower())
{
return Json(new { status = 2 }, JsonRequestBehavior.AllowGet);
}
}
var newSong = new Song();
newSong.UserName = User.Identity.Name;
newSong.Title = song.Title;
newSong.YoutubeLink = song.YoutubeLink;
newSong.GenreId = genre;
newSong.Date = DateTime.Now;
repository.AddSong(newSong);
var point = new Point();
point.UsernameGotPoint = song.UserName;
point.UsernameGavePoint = User.Identity.Name;
point.Date = DateTime.Now;
point.Score = 1;
point.TopTenFavId = id;
repository.AddPoint(point);
repository.Save();
return Json(new { status = 1 }, JsonRequestBehavior.AllowGet);
}
else
{
return Json(new { status = 4 }, JsonRequestBehavior.AllowGet);
}
}
else
{
return Json(new { status = 3 }, JsonRequestBehavior.AllowGet);
}
}
我还注册了我的新路线:
routes.MapRoute(
"AddPoint", // Route name
"{controller}/{action}/{songId},{genreId}", // URL with parameters
new { controller = "Home", action = "", songId = UrlParameter.Optional, genreId = UrlParameter.Optional } // Parameter defaults
);
500表示通常在处理请求时发生异常。使用fiddler检查http结果,并显示异常消息/堆栈跟踪。只需删除您的路由并保留默认路由,然后查看发生的情况。我认为genreId不是int500,这通常意味着在处理请求时发生异常。使用fiddler检查http结果并显示异常消息/堆栈跟踪。只需删除路由并保留默认路由,然后查看发生的情况我认为genreId不是int