简体   繁体   English

如何: (a) 从浏览器发送 JSON 数据到 controller; (b) 将转换后的数据发送到 ASP.NET MVC 中的 SQL 服务器?

[英]How to: (a) send JSON data from the browser to the controller; and (b) send the transformed data to SQL Server within ASP.NET MVC?

I have a form which includes a variety of <input> elements and makes use of 1-to-n tabulator tables for data input.我有一个表单,其中包含各种<input>元素,并使用 1 对 n 制表符表进行数据输入。 I have managed to successfully assemble data from these elements into a JSON string.我已经成功地将来自这些元素的数据组装成一个 JSON 字符串。 I am now attempting to complete the following two steps:我现在正在尝试完成以下两个步骤:

  1. Using Ajax, post the JSON object to my web server;使用 Ajax,将 JSON object 发布到我的 web 服务器; and
  2. In the ASP.NET MVC controller, upload the deserialized JSON data into a SQL Server 2016 table.在 ASP.NET MVC controller 中,将反序列化的 JSON 数据上传到 SQL Server 2016 表中。

My client-side script to POST the JSON object is as follows:我发布 JSON object 的客户端脚本如下:

var myJson = "the data from the form elements is programmatically inserted into the JSON string";

// use parseJSON() to test the syntax
try {
    var obj = jQuery.parseJSON(myJson);
}
catch(error) {
    console.log(error);
}
$.ajax({
        type: "POST",
        url: "/Dailies/UploadJson/",
        dataType: 'json',
        data: JSON.stringify(myJson),
        contentType: 'application/json',
        crossDomain: true,
        cache: false,
        success: function(data) { console.log(data); }
});

The method called within my ASP.NET MVC controller appears as follows:在我的 ASP.NET MVC controller 中调用的方法如下所示:

[HttpPost]
public IActionResult UploadJson(Object jsonFile)
{
    // insert data into SQL Server table
}

Note: I have already created the appropriate domain model within my ASP.NET MVC app and have also added a DbSet reference to the DbContext model. I have verified my ability to insert rows into the SQL Server table using mock data.注意:我已经在我的 ASP.NET MVC 应用程序中创建了适当的域 model,并且还添加了对DbContext model 的DbSet引用。我已经验证了我使用模拟数据将行插入到 SQL 服务器表中的能力。

When I place a breakpoint inside the UploadJson() method, I find that the jsonFile object is null.当我在UploadJson()方法中放置一个断点时,我发现 jsonFile object 是 null。

My quandry at this point is two-fold:在这一点上,我的困惑有两个:

  1. I can't seem to get JSON data from the client to the web server;我似乎无法从客户端获取JSON数据到web服务器; and
  2. I need to better understand how to transform the JSON data (once received) for upload into my database.我需要更好地了解如何转换 JSON 数据(一旦收到)以上传到我的数据库中。

Any assistance is greatly appreciated.非常感谢任何帮助。

Although there are plenty of questions related to this, the answers to those typically refer to binding to a model instead of just the json string.尽管有很多与此相关问题,但这些问题的答案通常是指绑定到 model 而不仅仅是 json 字符串。 But those will also help you.但这些也会帮助你。

It looks like there are two things:看起来有两件事:

  1. I would change the controller to receive a string instead of an object.我会更改 controller 以接收string而不是 object。
  2. You'll need to update the json data you're passing to the controller to match the parameter name of the controller. So in this case, the controller would receive a parameter named jsonFile .您需要更新传递给 controller 的 json 数据,以匹配 controller 的参数名称。因此,在这种情况下,controller 将接收名为jsonFile的参数。 So in the $.ajax method you'll want update the data to something like:因此,在$.ajax方法中,您需要将data更新为如下内容:
data: { jsonFile: JSON.stringify(myJson) }

UPDATE:更新:

  1. remove the Content-Type of application/json删除application/json的 Content-Type

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Send JSON data to highcharts pie from asp.net MVC controller in C# - Send JSON data to highcharts pie from asp.net MVC controller in C# 将JSON数据从Sencha Touch发送到ASP.NET MVC - Send JSON data from Sencha Touch to ASP.NET MVC 如何将自定义字符串与json数据一起发送到asp.net mvc控制器? - How would I send custom string with json data to asp.net mvc controller? 如何一次性将Angular中的json数据和formdata(文件)发送到ASP.NET WebAPI Controller操作? - How to send json data and formdata (files) from Angular to an ASP.NET WebAPI Controller action in one shot? 如何从ASP.Net MVC控制器发布JSON数据? - How to post JSON data from ASP.Net MVC controller? 如何将JSON数组发送到ASP.NET MVC控制器 - How to send JSON Array to an ASP.NET MVC Controller 将表单数据作为对象数组发送到 asp.net mvc 中的控制器 - Send form data as array of objects to controller in asp.net mvc 数据表Ajax数据:将Json数组发送到ASP.NET控制器 - Datatables Ajax Data: Send Json Array to ASP.NET Controller 从控制器返回Json数据以查看ASP.NET MVC - Returning Json Data From Controller to View ASP.NET MVC 如何将JSON数据从ASP.NET MVC 5发送回客户端Angular(即在身份验证期间) - How to send JSON data from ASP.NET MVC 5 back to client side Angular (i.e. during authentication)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM