【问题标题】:Ajax not passing strings or arrays to MVC ControllerAjax 未将字符串或数组传递给 MVC 控制器
【发布时间】:2014-09-24 09:47:07
【问题描述】:

我有这个 jQuery 方法

var params = {
    pGroupIdents: $.param({ pGroupIdents: groupIdents }),
    pIsNew: isNew,
    pNewTypeCategoryIdent: $(DetailsNewTypeCategory_Select).val(),
    pNewTypeTitle: $(DetailsNewTypeTitle_TB).val(),
    pExistingTypeIdent: $(DetailsExistingTypeCategory_Select).val(),
    pNote: isNew ? $(DetailsNewTypeNote_TB).val() : $(DetailsExistingTypeNote_Select).val()
};
var json = JSON.stringify(params);
$.ajax({
    url: "/Activities/PostExtraCurricular/SetExtraCurricular/",
    type: "POST",
    data: json,
    dataType: "json",
    processData: false,
    contentType: "application/json; charset=utf-8"
})
    .always(function () {
    })
    .success(function (data) {
    })
    .fail(function () {
    });

发布到这个方法

    public JsonResult SetExtraCurricular(Int32[] pGroupIdents, bool pIsNew, int pNewTypeCategoryIdent, String pNewTypeTitle, int pExistingTypeIdent, String pNote)

而字符串化的数据是这样的

"{"pGroupIdents":["12033","12025","12030"],"pIsNew":true,"pNewTypeCategoryIdent":"2","pNewTypeTitle":"title","pExistingTypeIdent":"2","pNote":"note"}"

在控制器中int 参数工作正常,但String 参数和Int32[] 参数始终为空。

我试过了;添加[HttpPost]traditional: truestring vs Stringint[] vs Int32[] vs IEnumerable[int]

我以前做过这一切,只是花花公子,但我不知道这里有什么不工作

【问题讨论】:

    标签: jquery ajax asp.net-mvc json post


    【解决方案1】:

    您不应该对参数进行字符串化。只需通过对象发送就可以了。

    $.ajax({
        url: "/Activities/PostExtraCurricular/SetExtraCurricular/",
        type: "POST",
        data: params,
        dataType: "json",
        processData: false,
    })
    

    【讨论】:

      【解决方案2】:

      已解决,为此我创建了一个包装类来传递我的变量,就像我之前所做的那样,但是对于类似的问题,它由客户端的parseInt() 解决了,我猜是这里的问题也是。

      【讨论】:

        猜你喜欢
        • 2016-07-18
        • 2017-10-23
        • 2018-09-01
        • 1970-01-01
        • 2016-11-08
        • 2017-03-11
        • 1970-01-01
        • 1970-01-01
        • 2021-08-02
        相关资源
        最近更新 更多