如何通过ajax将对象数组传递给asp.net核心控制器 [英] How to pass array of objects to asp.net core controller via ajax

查看:46
本文介绍了如何通过ajax将对象数组传递给asp.net核心控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在处理一个复杂的表单,其中包含从不同来源提取数据的不同部分.表单有复选框、选择选项和输入表单.

I am working on a complex form with different sections that are pulling data from different sources. the form has checkboxes, select options, and input forms.

表单具有动态输入字段.所以我决定获取所有输入字段并序列化在下面的代码中看到.我能够提取所有输入数据.

There form has dynamic input fields. So I decided to get all the input fields and serialize has seen in the code below. I was able to pull all the input data.

我尝试了字典和参数类型,但仍然得到空或空参数.

I tried dictionary and parameter type but still getting the empty or null parameter.

我在 StackOverflow 上尝试了一些建议,但没有解决问题.

I have tried some suggestions here on StackOverflow but non has been able to fix the issue.

当我把它传递给控制器​​时,我得到了空/空

When I passed this to the controller I got null/empty

如何将对象数组传递给控制器​​?

How can I pass the array of objects to the controller?

控制器图片如下

下面的代码

$(function () {
    $("#btn-down-save").on("click", function () {;
        var serialised = $('form').serializeArray();
        console.log("serialised data", serialised);

        $.ajax({
            "type": "POST",
            "url": "/MerchantFeeSetup/CreateMerchantFee",
            "dataType": "json",
            "contentType": "application/json",
            "data": JSON.stringify(serialised),
            success: function (result) { }
        })
    })
})

控制器

[HttpPost]
public ActionResult CreateMerchantFee([FromBody] List<string> collection)
{
}

推荐答案

试试这个:

创建视图模型

public class NameValue
{
public string Value {get; set;}
public string Name {get; set;}
}

修复ajax

  $.ajax({
            type: "POST",
            url: "/MerchantFeeSetup/CreateMerchantFee",
            data: {"collection":serialised},
            success: function (result) { }
        })

并删除 [FromBody]

and remove [FromBody]

[HttpPost]
public ActionResult CreateMerchantFee( List<NameValue> collection)
{
}

这篇关于如何通过ajax将对象数组传递给asp.net核心控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆