我正在尝试将带有序列化形式的数组作为参数从ajax发送到控制器,但第二个参数“test”是给出空值

发布于 2025-01-09 00:48:13 字数 1359 浏览 2 评论 0原文

$("#CreateDepartmentForm").submit(function (e) { e.preventDefault();

        var modelx = $(this).serialize();
        console.log(modelx);
        var cars = [
            {
                "color": "purple",
                "type": "minivan",
                "registration": "2018-03-03",
                "capacity": 7
            },
            {
                "color": "red",
                "type": "station wagon",
                "registration": "2018-03-03",
                "capacity": 20
            }];
       
        $.ajax({
            type: "POST",
            url: "/Department/CreateDepartmentByAjax",
            data: modelx + "&test=" + cars,
            success: function (res) {
            },
         });

public IActionResult CreateDepartmentByAjax(DepartmentVM model,List<cars> test)
    {
     }

   public  class cars
{
    public string color  { get; set; }
    public string type { get; set; }
    public string registration { get; set; }
    public int capacity { get; set; }
}

Ajax 代码图像`

-----------

我正在尝试将具有序列化形式的数组作为参数从ajax发送到控制器,但第二个参数“test”给出空值 所以,问题是控制器没有得到参数测试,我尝试了很多示例和教程,但没有任何帮助。也许我遗漏了一些简单明显的错误?如何将参数从 ajax 函数传递到控制器?

$("#CreateDepartmentForm").submit(function (e) {
e.preventDefault();

        var modelx = $(this).serialize();
        console.log(modelx);
        var cars = [
            {
                "color": "purple",
                "type": "minivan",
                "registration": "2018-03-03",
                "capacity": 7
            },
            {
                "color": "red",
                "type": "station wagon",
                "registration": "2018-03-03",
                "capacity": 20
            }];
       
        $.ajax({
            type: "POST",
            url: "/Department/CreateDepartmentByAjax",
            data: modelx + "&test=" + cars,
            success: function (res) {
            },
         });

public IActionResult CreateDepartmentByAjax(DepartmentVM model,List<cars> test)
    {
     }

   public  class cars
{
    public string color  { get; set; }
    public string type { get; set; }
    public string registration { get; set; }
    public int capacity { get; set; }
}

Ajax Code Image`

-----------

I'm trying To Send Array with Serialize Form as a parameter from ajax to controller but the second parameter "test" giving null value
So, the problem is that controller doesn't get the parameter test, I've tried many examples and tutorials but nothing helped. maybe there's some easy obvious mistake which I am missing? How could I pass parameters from ajax function to controller?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

梦境 2025-01-16 00:48:13

通过将对象序列化为 JSON 将对象传递给 API。

$("#CreateDepartmentForm").submit(function (e) {
    e.preventDefault();

    var modelx = {};
    $(this).children("input").each(function () {
        var name = $(this).attr('name');
        var value = $(this).val();
        modelx[name] = value;
    });

    ...
      

    $.ajax({
        type: "POST",
        url: "/Department/CreateDepartmentByAjax",
        contentType: "application/json",
        data: JSON.stringify({ model: modelx, test: cars }),
        success: function (res) {
        },
    });
});

并修改控制器:

public class CreateDepartmentInput
{
    public DepartmentVM model { get; set; }
    public List<cars> test { get; set; }
}
[HttpPost]
public IActionResult CreateDepartmentByAjax([FromBody]CreateDepartmentInput input)
{
    ...
}

请求负载

请求有效负载

Visual Studio 调试输出

Visual Studio 调试输出

Pass the object to API by serializing the object as JSON.

$("#CreateDepartmentForm").submit(function (e) {
    e.preventDefault();

    var modelx = {};
    $(this).children("input").each(function () {
        var name = $(this).attr('name');
        var value = $(this).val();
        modelx[name] = value;
    });

    ...
      

    $.ajax({
        type: "POST",
        url: "/Department/CreateDepartmentByAjax",
        contentType: "application/json",
        data: JSON.stringify({ model: modelx, test: cars }),
        success: function (res) {
        },
    });
});

And modify the controller:

public class CreateDepartmentInput
{
    public DepartmentVM model { get; set; }
    public List<cars> test { get; set; }
}
[HttpPost]
public IActionResult CreateDepartmentByAjax([FromBody]CreateDepartmentInput input)
{
    ...
}

Request Payload

Request Payload

Visual Studio Debugging Output

Visual Studio Debugging Output

人生戏 2025-01-16 00:48:13

如果您想为 POST 请求传递类似数组的查询参数,您需要在控制器方法中添加 [FromQuery] 属性来列出参数。相反,最好将 json 有效负载放入请求正文中。

If you want to pass an array like query parameter for POST request you need to add [FromQuery] attribute to list parameter in you controller method. Instead it's better to put json payload to request body.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文