简体   繁体   English

MVC:将对象和整数作为JSON发送到控制器

[英]MVC: Send object and integer to controller as JSON

I'm trying to send a custom object and an integer to my controller action using jQuery AJAX. 我正在尝试使用jQuery AJAX向我的控制器操作发送自定义对象和一个整数。

[HttpPost]
public JsonResult GetFilenameSuggestion(Document document, int tradeId)
{
    //do stuff...
}

In my JS, I've tried to create the json sent to the controller in several ways, including: 在我的JS中,我尝试以多种方式创建发送到控制器的json,包括:

var json = JSON.stringify({ document: document, tradeId: tradeId });
//or
var json = { document: JSON.stringify(document), tradeId: tradeId };
//or
var json = { document: document, tradeId: tradeId };

and here's my jQuery AJAX: 这是我的jQuery AJAX:

$.ajax({
        type: "POST",
        dataType: "json",
        contentType: "application/json; charset=utf-8",
        url: "/Document/GetFilenameSuggestion",
        data: json,
    error: function (XMLHttpRequest, textStatus, errorThrown) {
        //do bad stuff...
    },
    success: function (data) {
        //do good stuff...
});

Any suggestions on how to do this? 有关如何执行此操作的任何建议? I am getting an internal server error when the ajax posts, and I'm 99% sure it's due to how the arguments are being passed to the controller action. 我在ajax发布时收到内部服务器错误,我有99%的把握是由于参数如何传递到控制器操作。

Use JSON.stringify to convert your javascript object to it's json string version and specify the contentType as " application/json ". 使用JSON.stringify将您的JavaScript对象转换为json字符串版本,并将contentType指定为“ application/json ”。

The below code should work fine. 下面的代码应该可以正常工作。

var model = { document: { DocumentName: "Dummy" }, tradeId: 34 };

$.ajax({
    type: "POST",
    dataType: "json",
    contentType: "application/json; charset=utf-8",
    url: "/Document/GetFilenameSuggestion",
    data: JSON.stringify(model),
    error: function(XMLHttpRequest, textStatus, errorThrown) {
        //do bad stuff...
    },
    success: function(data) {
        //do good stuff...
    }
});

for this action method signature 为此操作方法签名

[HttpPost]
public JsonResult GetFilenameSuggestion(Document document, int tradeId)
{
    // to do : return some useful JSON data
}

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

相关问题 在MVC中使用Javascript将Json对象和文件发送到控制器 - Send Json object and file to controller with Javascript in MVC 如何使用ajax将嵌套的json对象发送到mvc控制器 - How to send nested json object to mvc controller using ajax 如何将JSON发送到MVC控制器? - How to send json to mvc controller? 是否可以将文件列表和FormData对象中的整个json对象发送到ASP.NET CORE MVC控制器 - Is it possible to send a list of files and a whole json object in a FormData object to a ASP.NET CORE MVC Controller 无法将JSON数据发送到MVC控制器 - Unable to send JSON data to MVC controller 如何在ASP.NET MVC控制器中正确发送和处理JSON对象? - How to properly send and handle a JSON object in an ASP.NET MVC controller? 如何将Json对象(或字符串数​​据)从Javascript xmlhttprequest发送到MVC Controller - How to send Json object (or string data) from Javascript xmlhttprequest to MVC Controller Angularjs null object 问题发送mvc controller - Angularjs null object problem to send mvc controller 将 JSON 对象从 rails 控制器发送到 Javascript - Send JSON object from rails controller to Javascript 如何将JSON对象发送到Controller JsonResult方法 - How to send json object to the Controller JsonResult Method
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM