[英]Passing javascript array from view to controller in ASP.Net MVC 5
[英]passing array from javascript to controller MVC 4
我正在使用剃刀,我很難將數組傳遞給控制器。 數組包含我制作的對象,我正在嘗試這樣做:
$.ajax({
type: "POST",
url: "HomePage/HandleOperations",
data: JSON.stringify(operationCollection),
success: function (data) { alert("SUCESS");},
dataType: "json",
contentType: "application/json"
});
我的控制器是:
public void HandleOperations(List<string> operationCollection)
{
}
我不需要使用ajax,但我不確定如何才能完成。 在控制器中,它顯示“operationCollection”包含元素,但它們都是null。
Ajax參數
traditional : true
會做的。
客戶端:
$.ajax({
type: "POST",
url: "HomePage/HandleOperations",
data: {operations: operationCollection},
success: function (data) { alert("SUCCESS"); }
});
並聲明類服務器端如下:
public class Operation
{
public int Index;
public string Source;
public string Target;
public int AddOrDel;
}
然后你可以采取這個行動:
public void HandleOperations(Operation[] operations)
{
}
使用傳統:ajax調用的true參數:
為了幫助radbyx,使用ajax調用的“traditional:true”屬性,如下所示,將告訴ajax使用傳統的序列化形式。 更多細節: http : //api.jquery.com/jQuery.param/或者什么是JQuery中的“傳統樣式的param序列化” 。
$.ajax({
type: "POST",
url: "HomePage/HandleOperations",
data: {operations: operationCollection},
traditional: true,
success: function (data) { alert("SUCCESS"); }
});
在ajax中添加“traditional:true”參數。
例:
var parentValueToPush=new Array();
$('[name=SelectedUsers]:checked').each(function () {
parentValueToPush.push($(this).val());
temp.push($(this).val());
});
$.ajax({
url: //URL,
type: 'get',
traditional: true,
dataType: 'html',
cache: false,
data: { SelectedUsers: parentValueToPush},
success: function (data) {
//Result
}
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.