C# jQuery 后数组 - ASP.Net MVC 4

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12572856/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 23:45:15  来源:igfitidea点击:

jQuery post array - ASP.Net MVC 4

c#jqueryasp.netasp.net-mvc

提问by Silent

I have spent 8 hours or so today trying to figure this out. I have viewed lots of solutions but cannot get the same results. I have a hunch it has everything to do with being relatively new to ASP.Net.

我今天花了 8 个小时左右的时间试图解决这个问题。我查看了很多解决方案,但无法获得相同的结果。我有一种预感,这与 ASP.Net 相对较新有关。

Here is the latest question I tried mimicking with no luck. https://stackoverflow.com/questions/10007722/post-array-as-json-to-mvc-controller#=

这是我尝试模仿但没有运气的最新问题。 https://stackoverflow.com/questions/10007722/post-array-as-json-to-mvc-controller# =

How to post an array of complex objects with JSON, jQuery to ASP.NET MVC Controller?

如何使用 JSON、jQuery 将一组复杂对象发布到 ASP.NET MVC 控制器?

Basic Rundown of Problem: I have an array of json objects I would like to pass to my controller. When I pass the data it shows lets say for example 3 items, but their values are not passed or it just shows nothing was passed. Firebug shows it passed it so I assume that something is not setup right and its not allowing it to set that variable up correctly on the C# side.

问题的基本概要:我有一组 json 对象,我想传递给我的控制器。当我传递它显示的数据时,可以说例如 3 个项目,但它们的值没有传递,或者它只是显示没有传递任何内容。Firebug 显示它通过了它,所以我假设某些东西没有正确设置并且它不允许它在 C# 端正确设置该变量。

I have tried a few things and ill list them below: Setup 1: I tried mocking what I seen at the second link:

我已经尝试了一些事情,并在下面列出了它们: 设置 1:我尝试嘲笑我在第二个链接中看到的内容:

$.ajax({
        type: 'Post',
        cache: false,
        url: '/Workflow/Home/UpdateStepPositions',
        data: { 'steps': ['1','2','3'] },
        async: false,
        success: function (data) {
            console.debug(data);
        },
        error: function (data) {
            console.debug(data);
        }
    });

 Controller
 [HttpPost]
    public ActionResult UpdateStepPositions(string[] steps){

        var bresults = new {
            Success = false,
            Message = "Unable to update step positions."
        };

        return Json(bresults);
    }

I couldn't even get that simple setup working.. It gets to the function and shows there was nothing passed....

我什至无法让那个简单的设置工作..它进入函数并显示没有任何通过......

Setup 2:

设置 2:

 list = new Array();
    list.push({ "step": 1, "position": 1 });
    list.push({ "step": 2, "position": 2 });
    list.push({ "step": 3, "position": 3 });

    $.ajax({
        type: 'Post',
        cache: false,
        url: '/Workflow/Home/UpdateStepPositions',
        data: JSON.stringify({ 'steps': list }),
        async: false,
        success: function (data) {
            console.debug(data);
        },
        error: function (data) {
            console.debug(data);
        }
    });

    Controller
   [HttpPost]
    public ActionResult UpdateStepPositions(List<UpdatedSteps> steps){
        var bresults = new {
            Success = false,
            Message = "Unable to update step positions."
        };

        return Json(bresults);
    }

   Class
   public class UpdatedSteps {
    public string Step { get; set; }
    public string Position { get; set; }
}

Can anyone shine some light on what I'm missing or point me in the right direction? Hopefully its something simple and just a newbie mistake!

任何人都可以照亮我所缺少的东西或指出我正确的方向吗?希望它的东西很简单,只是一个新手错误!

采纳答案by webdeveloper

MVC detects what type of data it receive by contentType. Here is working example:

MVC 通过 contentType 检测它接收的数据类型。这是工作示例:

$(function () {
    $.ajax({
        type: 'Post',
        dataType: 'json',
        url: '/Workflow/Home/UpdateStepPositions',
        data: JSON.stringify({ steps: ['1', '2', '3'] }),
        contentType: 'application/json; charset=utf-8',
        async: false,
        success: function (data) {
            console.debug(data);
        },
        error: function (data) {
            console.debug(data);
        }
    });
});

Now everything ok with request:

现在一切正常,请求:

Content-Type:        application/json; charset=utf-8
X-Requested-With:    XMLHttpRequest

and response:

和回应:

Content-Type:        application/json; charset=utf-8