【问题标题】:How send complex data type with ajax to the action in controller in ASP.NET MVC如何使用 ajax 将复杂数据类型发送到 ASP.NET MVC 中的控制器中的操作
【发布时间】:2020-12-14 07:37:11
【问题描述】:

在我的 ASP.NET 框架 MVC 项目中,我必须使用 ajax 向控制器中的操作发送以下两个数据(第一个数据是字符串列表,第二个数据是 GUID):

0: "{Key:'052c5941-233a-4bd2-80e1-7cfffa34ca44',Value:'Salary1'}"
1: "{Key:'00000000-0000-0000-0000-000000000004',Value:'Salary2'}"
2: "{Key:'00000000-0000-0000-0000-000000000005',Value:'Salary3'}"
3: "{Key:'00000000-0000-0000-0000-000000000003',Value:'Salary4'}"
4: "{Key:'00000000-0000-0000-0000-000000000001',Value:'Salary5'}"
5: "{Key:'00000000-0000-0000-0000-000000000002',Value:'Salary6'}"

"6a580cf1-2f05-4621-8a67-8fe0bdd559c2"

我的操作如下

 public JsonResult DigestFile(List<string> value1, string pointId)
  {
   ....
  }

我该怎么做?任何帮助都会得到帮助!

【问题讨论】:

  • 如果允许更改 api,我将创建特定的类来封装所有必需的属性。具有 2 个属性的类:一个是 Dictionary 类型,另一个是 Guid 类型

标签: javascript c# jquery ajax asp.net-mvc


【解决方案1】:

您可以使用 HTTP post 作为以下代码来执行此操作,您只需创建一个具有 Key 和 Value 的类,并在具有 String 变量的 Controller 中使用它,在 HTTP post 函数上。

我不是 C# 方面的专家,我确​​实从运行良好的 VB 代码在线翻译。

在您的视图中 (.cshtml):

<input id="Button1" type="button" value="button" onclick="SaveMe()" />
<script>
    function SaveMe() {
        var GUID = '6a580cf1-2f05-4621-8a67-8fe0bdd559c2';

        // Creat Object
        var lineItems = new Object();
        lineItems.Entrys = new Array();

        // Example Filling your object, but of course you can get data from another place ...
        lineItems.Entrys[0] = new Object({ Key: '052c5941-233a-4bd2-80e1-7cfffa34ca44', Value: 'Salary1' });
        lineItems.Entrys[1] = new Object({ Key: '00000000-0000-0000-0000-000000000004', Value: 'Salary2' });
        lineItems.Entrys[2] = new Object({ Key: '00000000-0000-0000-0000-000000000005', Value: 'Salary3' });


        $.ajax({
            type: "POST",
            url: "/Home/AjaxMethodDigestFile",
            data: JSON.stringify({ Entrys: lineItems.Entrys, GUID: GUID }),
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function (response) { alert(response.message); },
            failure: function (response) { alert("failure"); },
            error: function (response) { alert("error"); }
        });
    }
</script>

在您的模型中:

namespace MyModel1.ViewModel
{
    public class MyClass1
    {
        public string Key { get; set; }
        public string Value { get; set; }
    }
}

在您的控制器中:

[HttpPost]
public JsonResult  AjaxMethodDigestFile(ICollection<MyModel1.ViewModel.MyClass1> Entrys, string GUID)
{
    string message = "";
    int counter = 0;

    foreach (var entry in Entrys)
    {
        // How to use this data example
        string Key = entry.Key;
        string Value = entry.Value;
        counter += 1;
        message += Value + ": " + Key + Constants.vbCrLf;
    }

    // The following lines are not necessary, it's just an example code to know what happen and return it to client side ...
    if (counter > 0)
        message = counter.ToString() + " Entrys received" + Constants.vbCrLf + message;
    else
        message = "no entrys";


    var response = new { counter, message };
    
    return Json(response);
}

【讨论】:

  • 谢谢你,太好了,你帮我解决了我的问题。但是我在 Action 中做了一些更改,我的意思是“Entrys”的输入是一个 null 列表,我不知道,我没有使用模型“MyClass1”,我的 Action 的输入是:public ActionResult DigestFile( List 映射,字符串 pointId){...}
猜你喜欢
  • 2012-02-22
  • 2012-07-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-03-07
  • 2016-03-28
  • 2015-02-09
  • 1970-01-01
相关资源
最近更新 更多