ajax POST asp.net 核心中的 int 参数

ajax POST int parameter in asp.net core

我正在将我的 MVC 项目迁移到 Core,我一直很难修复所有旧的 ajax 调用。

我可以将模型和字符串参数传递到控制器中,但是,整数对我不起作用。

我可以将它们作为字符串参数包装到 JSON 对象中,例如控制器中的 [FromBody]string objId,但是我必须从 Json {'objId' : 1}

有什么方法可以避免这种情况,只传递一个整数吗?

下面是我正在尝试的代码。

[HttpPost]
public JsonResult PassIntFromView([FromBody]int objId)
{
    //DO stuff with int here
}

这是js。

var data = { "objId": 1};
    $.ajax({
        url: '@Url.Action("PassIntFromView", "ControllerName")',
        data: JSON.stringify(data),
        type: "POST",
        dataType: 'JSON',
        contentType: "application/json",
        success: function(data) {
            //do stuff with json result 
        },
        error: function(passParams) {
            console.log("Error is " + passParams);
        }
    });

控制器中的objId始终为0

我试过这个但没有做 JSON.stringify(data) 也没有结果。

我也尝试了所有不同的表单属性变体。

试试这个:

var data = { "objId": 1};
$.ajax({
    url: '@Url.Action("PassIntFromView", "ControllerName")',
    data: data,
    type: "POST",
    dataType: 'JSON',
    contentType: "application/json",
    success: function(data) {
        //do stuff with json result 
    },
    error: function(passParams) {
        console.log("Error is " + passParams);
    }
});

您的控制器:

[HttpPost]
public JsonResult PassIntFromView(int objId)
{
    //DO stuff with int here
}

JSON 偏好字符串而不是整数。您最好使用 JSON.stringify(data) 来解析您的控制器,将其转换为控制器中的整数,然后解析返回的字符串:

var data = { objId: 1};
$.ajax({
    url: '@Url.Action("PassIntFromView", "ControllerName")',//asp.net - url: 'api/controllerName/controllerFunction'
    data: JSON.stringify(data),
    type: "POST",
    dataType: 'JSON',
    contentType: "application/json",
    success: function(data) {
        var result = JSON.parse(data);
        //do stuff with json result 
    },
    error: function(passParams) {
        console.log("Error is " + passParams);
    }
});

尝试使用 contentType 作为 'application/x-www-form-urlencoded':

 var data = { objId: 1 };
 $.ajax({
     url: '@Url.Action("PassIntFromView", "ControllerName")',
     type: "post",
     contentType: 'application/x-www-form-urlencoded',
     data: data,
     success: function (result) {
         console.log(result);
     }
 });

然后移除控制器中的[FromBody]属性

[HttpPost]
public JsonResult PassIntFromView(int objId)
{
    //Do stuff with int here
}

Js

var data = { "objId": 1};

$.ajax({

    url: "ControllerName/PassIntFromView",

    data: data,

    type: "POST",

    dataType: 'JSON',   

    success: function(data.result!=null) {

    console.log(data.result);

    },
    error: function(passParams) {
        console.log("Error is " + passParams);
    }

});

我认为您的问题可能是您正在将一个对象传递给 api,但试图将其转换为基元。我知道已经有一个选择的答案,但试一试。

var data = { };
data["objId"] = 1; //I just wanted to show you how you can add values to a json object
$.ajax({
    url: '@Url.Action("PassIntFromView", "ControllerName")',
    data: JSON.stringify(data),
    type: "POST",
    dataType: 'JSON',
    contentType: "application/json",
    success: function(data) {
        //do stuff with json result 
    },
    error: function(passParams) {
        console.log("Error is " + passParams);
    }
});

您创建了一个模型class

public class MyModel {
     public int ObjId {get;set;}
}

您的控制器应该期望其中之一

[HttpPost]
public JsonResult PassIntFromView([FromBody] MyModel data)
{
    //DO stuff with int here
}

我是这样工作的

let numberFour = JSON.stringify(4);

$.ajax({
    .......
    .......
    data: numberFour,
    type: "POST",
    dataType: 'JSON',
    contentType: "application/json",
    ......
    ........
});