如何将字典项从客户端(Ajax)传递到服务器端(MVCAPI)

如何将字典项从客户端(Ajax)传递到服务器端(MVCAPI),ajax,asp.net-mvc,Ajax,Asp.net Mvc,我想使用post方法将字典类型的多个键和值对从客户端传递到服务器端,服务器端是MVCWebAPI HTTP get方法 function FileSenderAPI(){ var DictionaryData = new Object; DictionaryData = document.getElementById("hidFilePath").value; var Url = '<%=System.Confi

我想使用post方法将字典类型的多个键和值对从客户端传递到服务器端,服务器端是MVCWebAPI HTTP get方法

   function FileSenderAPI(){
            var DictionaryData = new Object;
            DictionaryData = document.getElementById("hidFilePath").value;
            var Url = '<%=System.Configuration.ConfigurationManager.AppSettings["FileSenderAPI"].To String() %>';
            $.ajax({
                url: Url,
                method: 'Get',
                data Type: "json",
                data: {
                    ModelsPath:JSON.stringify(DictionaryData),
                    Exchange: exchange,
                    Exchange_key: key,
                },
                success: function (data, textStatus, xhr) {
                    alert(data);
                },
}
函数filesendrapi(){
var DictionaryData=新对象;
DictionaryData=document.getElementById(“hidFilePath”).value;
var Url='';
$.ajax({
url:url,
方法:“Get”,
数据类型:“json”,
数据:{
ModelsPath:JSON.stringify(字典数据),
交换:交换,,
交换密钥:密钥,
},
成功:函数(数据、文本状态、xhr){
警报(数据);
},
}
公共HttpResponseMessage转换模型(字典模型SPATH、字符串交换、字符串交换\u键)
{
}//这是我的API方法``

这里有一个解决方案。你可以试试。希望能帮上忙,我的朋友:))

1) 创建模型

public class DictionaryModel
    {
        public Dictionary<string, string> dict { get; set; }
        public string Exchange { get; set; }
        public string Exchange_Key { get; set; }

    }
3) 鉴于

public class DictionaryModel
    {
        public Dictionary<string, string> dict { get; set; }
        public string Exchange { get; set; }
        public string Exchange_Key { get; set; }

    }
        [HttpPost]
        public JsonResult Example(DictionaryModel model)
        {
            // Your Logic
            return Json("Success");
        }
$('#btClick').on('click', function () {
                var dict = {};
                dict["id"] = "200";
                dict["Name"] = "Chris";
                dict["DynamicItem1"] = "Item 1";


                var theObject = {};
                theObject.dict = dict;
                theObject.Exchange = "Abc";
                theObject.Exchange_Key = "123";

                let url = '@Url.Action("Example","Home")';
                $.post( url, theObject, function (data, textStatus, XMLHttpRequest) {
                    console.log("success");
                }, "json");
            });