jQuery的岗位阵列 - ASP.Net MVC 4 [英] jQuery post array - ASP.Net MVC 4
问题描述
我已经花了8个小时左右今天试图弄清楚这一点。我已经看到很多的解决方案,但不能得到相同的结果。我有它的一切做是比较新的ASP.Net预感。
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.
下面是我试过,没有运气模仿的最新问题。
=>张贴数组作为JSON到MVC控制器
Here is the latest question I tried mimicking with no luck. =">Post Array as JSON to MVC Controller
<一个href=\"http://stackoverflow.com/questions/320291/how-to-post-an-array-of-complex-objects-with-json-jquery-to-asp-net-mvc-control\">How发布使用JSON,jQuery的复杂对象的数组ASP.NET MVC控制器?
问题的基本流程:
我有JSON对象的数组,我想传递给我的控制器。当我通过它显示的数据可以说,例如3个项目,但它们的值不通过或它只是显示没有获得通过。萤火虫显示它通过它,所以我认为东西是不正确的设置和它不允许它正确地设置该变量了C#的一面。
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.
我已经尝试了一些东西,生病一一列举如下:
设置1:
我试图嘲弄我的第二个链接看到:
I have tried a few things and ill list them below: Setup 1: I tried mocking what I seen at the second link:
$.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....
设置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!
推荐答案
MVC检测到它的contentType所接收的数据类型。在这里工作的例子:
MVC detects what type of data it receive by contentType. Here is working example:
$(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);
}
});
});
现在一切OK与要求:
Content-Type: application/json; charset=utf-8
X-Requested-With: XMLHttpRequest
和响应:
Content-Type: application/json; charset=utf-8
这篇关于jQuery的岗位阵列 - ASP.Net MVC 4的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!