отправка нескольких параметров asp.net mvc jquery ajax - PullRequest
2 голосов
/ 27 декабря 2011

Я получаю сообщение об ошибке сервера: состояние 500, когда я пытаюсь отправить 2 параметра своему действию в контроллере с jquery ajax, не знаю почему?

это код jquery:

 $(".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
    );
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...