0

我对控制器中的函数进行了 ajax 调用。我需要向该函数传递一个参数,但它没有发生。我的ajax调用:

 $("#BTN_Plus").click(function () {
     var CurrentPage = @Model.CurrentPage;
    $.ajax({
        type: "POST",
        url: "/Accueil/ListerIncidentsHotline",
        data: JSON.stringify(CurrentPage),
        contentType: 'application/json; charset=utf-8',
        success: function (data) {
            $("#Affichage").html(data);
        }
    });
});

我的控制器中的功能:

public PartialViewResult ListerIncidentsHotline( int page = 1)
    {
            int NextPage = 1 + page;
            const int PageSize = 10;
            int NumDossier = StructureData.DonneNumDossier((string)Session["Utilisateur"], (string)Session["MotDePasse"]);
            IEnumerable<IncidentHotline> ListeIncidents = StructureData.DonneIncidentsHotline(NumDossier);
            int NbreIncidents = StructureData.DonneNombreIncidents(NumDossier);
            ViewBag.NombreIncidents = NbreIncidents;
            var viewModel = new IncidentsPageInfo()
            {
                NumPages = (int)Math.Ceiling((double)ListeIncidents.Count() / PageSize),
                CurrentPage = NextPage,
                PageSize = PageSize,
                Incidents = ListeIncidents.Skip((NextPage - 1) * PageSize).Take(PageSize),
            };
            return this.PartialView(viewModel);

    }

当我对变量CurrentPage发出警报时,它向我显示了正确的值,但是当涉及到 ajax 调用时,我收到一个错误,指出参数为空。无法弄清楚这有什么问题。

4

3 回答 3

1

well as a data you need to pass a numerable value with the name of page, change your ajax data

$.ajax({ type: "POST", url: "/Accueil/ListerIncidentsHotline", data: JSON.stringify(CurrentPage), contentType: 'application/json; charset=utf-8', success: function (data) { $("#Affichage").html(data); } });

change data to data: {page: CurrentPage}

于 2013-08-14T09:25:19.393 回答
0

显然问题来自我的ajax调用。所以我必须在 JSON.Stringify 方法中将参数的名称与其值连接起来,如下所示:

$("#BTN_Plus").click(function () {
    var CurrentPage = @Model.CurrentPage;
    alert(CurrentPage);
    $.ajax({
        type: "POST",
        url: "/Accueil/ListerIncidentsHotline",
        data: JSON.stringify({ page: CurrentPage }),
        contentType: 'application/json; charset=utf-8',
        success: function (data) {
            $("#Affichage").html(data);
        }
    });
});
于 2013-08-14T10:04:37.720 回答
0

这要容易得多......

$.get("/Accueil/ListerIncidentsHotline", { CurrentPage: @Model.CurrentPage } ).
  done(function (data) {
    $("#Affichage").html(data);
   });
于 2013-08-14T09:31:03.910 回答