简体   繁体   English

将js中的json传递给控制器

[英]Pass json from js to controller

Data doesn't passing to controller, always null 数据不传递给控制器​​,始终为null

My script in view: 我的脚本视图:

function deleteRecords() {
    var selected = [];
    $('#myTable input:checked').each(function () {
        selected.push($(this).attr('id'));
    });

    $.ajax({
        url: '/Home/DeleteRecords',
        type: 'POST',
        dataType: 'json',
        contentType: 'application/json; charset=utf-8',
        data: { "json": JSON.stringify(selected) },
        error: function () {
            alert("Error!");
        }
    });
}

My home controller method: 我的家庭控制器方法:

[HttpPost]
public IActionResult DeleteRecords(string AllId)
{
    return null;
}

send ajax request data like below, 发送ajax请求数据,如下所示,

$.ajax({
    url: '/Home/DeleteRecords',
    type: 'POST',
    contentType: 'application/json; charset=utf-8',
    data: JSON.stringify(selected),
    error: function () {
        alert("Error!");
    }
});

and receive the data in action like 并接收实际的数据

[HttpPost]
public IActionResult DeleteRecords(string[] AllId)
{
    return null;
}

It need to pass the action. 它需要通过动作。 Hope it helps to you. 希望对您有帮助。

Name your property in the Post the same as your method so that the automatic binding picks it up. 在Post中将您的属性命名为与您的方法相同的名称,以便自动绑定将其选中。 - Turns out this doesn't matter for single object. -事实证明,这对于单个对象无关紧要。

The data Object you were creating was not parse-able by .net, use JSON.stringify directly for the data payload. .net无法解析您正在创建的数据对象,请将JSON.stringify直接用于数据有效负载。

Note: The change in Home controller since you are passing an array of string. 注意:由于要传递字符串数组,因此Home控制器中的更改。

function deleteRecords() {
var selected = [];
$('#myTable input:checked').each(function () {
    selected.push($(this).attr('id'));
});

$.ajax({
    url: '/Home/DeleteRecords',
    type: 'POST',
    dataType: 'json',
    contentType: 'application/json; charset=utf-8',
    data: JSON.stringify(selected),
    error: function () {
        alert("Error!");
    }
});

} }

For your home controller method: 对于家庭控制器方法:

[HttpPost]
public IActionResult DeleteRecords(string[] AllId)
{
    return null;
}

with the code in your question, try below to get the json 加上您问题中的代码,请尝试以下操作以获取json

System.Web.Context.Current.Request.Form["json"]

if you want some more graceful stuff, you need to put FromBody attribute in your parameter signature 如果您需要更多优美的东西,则需要在参数签名中放入FromBody属性

DeleteResults([FromBody] string json)

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

相关问题 如何将JSON数据从C#控制器传递到angular js? - How to pass json data from C# controller to angular js? 将XML从控制器传递到JS - Pass xml from controller to js 在路由时在不使用服务或Factory的情况下将JSON数据从控制器传递到另一个控制器 - Pass the JSON Data Between From Controller To another Controller with out using Services or Factory in angular JS while Routing JSON从JS到MVC控制器 - JSON from js to MVC controller 如何从写在另一个视图上的JS代码重定向到控制器,并与之一起传递JSON对象 - How to redirect to a controller from JS code written on another view and pass JSON object along with it 如何从ASP.NET MVC中的控制器传递JSON中的chart.js图表​​数据对象 - How to pass a chart.js chart data object in json from a controller in asp.net mvc 如何将Json字符串从MVC控制器传递到d3.js图表​​? - How to pass a Json String from mvc controller to a d3.js chart? 如何将JSON对象从控制器传递到路由器? - How to pass json object from controller to router? 将JSON数组从控制器传递到JavaScript中的变量 - Pass JSON array from a controller to a variable in JavaScript 如何将JSON数据从控制器传递到JavaScript - How to pass json data from the controller to javascript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM