【问题标题】:.NET Core 6 handle int on controller.NET Core 6 在控制器上处理 int
【发布时间】:2022-11-10 23:55:24
【问题描述】:

我有一个基本的 apicontroller。我可以处理我设计中的任何模型。但我无法处理 int 或 string。

有我的代码

[Route("Get"),HttpPost]
        public async Task<JResultModel> Get(int id)
        {
            if (id == -1)
            ....
            return _JResult.Control(model);
        }

和 AJAX 帖子

$.ajax({
        url: postUrl,
        data: JSON.stringify(data),
        contentType: 'application/json',
        type: 'POST',
        success: function (resultData) {
            console.log(resultData);
            return resultData;
        }
    });

我想同时获得模型和类型。

【问题讨论】:

  • **** postUrl 是动态字符串。我可以触发我的控制器。没有问题。
  • 你的数据是什么样的?你能分享你关于数据的代码吗?
  • 数据是一个简单的对象。它可以是整数、数组或模型。我想通用。
  • 题外话:你用method:POST打电话,你用[HttpPost]标记,但你把它命名为......得到?
  • 你的data必须匹配您的参数 - 即data: { id: 123 }(或 JSON'd,如果需要)。如果您的参数是int id,则它不能是“通用”

标签: javascript jquery .net ajax asp.net-core


【解决方案1】:

如果没有模型,那么您甚至没有显式键入 dataType 或 contentType 而是这样做。

  ** $.ajax({
    type: 'POST',
    url: "Your Url",
    data: {
        ParameterNameInController: Value
    },
    success: function (result) {
       console.log(result)
        })
    },
    Error: function (error) {
        console.log(error);
    }
})**

【讨论】:

    猜你喜欢
    • 2017-06-18
    • 1970-01-01
    • 2022-07-15
    • 1970-01-01
    • 2023-03-16
    • 1970-01-01
    • 1970-01-01
    • 2019-12-31
    • 1970-01-01
    相关资源
    最近更新 更多