将 key-value 对数组传递给 .NET 中的控制器操作
Passing key-value pair array to Controller action in .NET
我有一个 key-value 对,我通过它发送如下值:
$(document).on('click', '.chkAll', function () {
$("#tableProducts thead tr th input:checkbox").change(function () {
$(".chkItem").prop('checked', $(this).prop("checked"));
if ($(this).prop('checked')) {
$('tbody tr td input[type="checkbox"]').each(function () {
product_ids[$(this).attr('id')] = "Checked";
productLocation[$(this).attr('id')] = $(this).attr('location');
});
} else {
$('tbody tr td input[type="checkbox"]').each(function () {
delete product_ids[$(this).attr('id')];
delete productLocation[$(this).attr('id')];
});
}
});
这就是我 post .NET MVC 操作中所有键的方式:
values: Object.keys(product_ids)
并在 Controller 中获取它:
public ActionResult GetValues(List<string> values)
{
// now I have all the keys here but now I'd like values as well...
}
我想象的方法是将整个 name-value 对数组直接传递到函数中,然后获取它,这样我就可以:
// key (product id) => 0124914815DD29
// value => checked
两个字符串 ...
我试过这样做:
values: product_ids // in jquery post
然后像这样获取值:
public ActionResult GetValues(Dictionary<string,string> values)
{
// but now the values dictionary is always empty
}
我该怎么做?
编辑:
这是数据所在的部分 posted:
$(".btnAnalyze").click(function () {
if (jQuery.isEmptyObject(product_ids) == true) {
ShowMessage("You haven't selected any product!");
return;
}
else {
var guid = '@Guid.NewGuid()'
var postData = { values: Object.keys(product_ids), drange: $('.range').val(), ID: guid, type: $('input[name=type]:checked').val(), shipping: $('input[name=shipping]:checked').val(), condition: $('input[name=condition]:checked').val(), minprice: $('.txtmin').val(), maxprice: $('.txtmax').val(), Title: $('.txtSearch').val(), negative: $('.txtNegativeKeywords').val(),locations: productLocation };
$.ajax({
type: "POST",
url: "/Analyze/GetAllProducts",
data: postData,
success: function (data) {
if (data == "Error") {
alert("Something went wrong");
} else if (data=="Ok"){
window.location.href = '/Analyze/Index/' + guid // redirect to another page
}
},
dataType: "json",
traditional: true
});
}
});
并映射 header 的动作:
[HttpPost]
public async Task<JsonResult> GetAllProducts(List<string> values, string drange, string ID, string type, string shipping, string condition, string minprice, string maxprice, string Title,string negative,string minFeedback, string maxFeedback, Dictionary<string,string> locations)
@Haim 请看我传递给 function 的最后一个参数,这就是我试图变成字典的参数...
显然,默认模型绑定器在尝试绑定 Dictionary
时需要以下格式:
locations[1]=val1&locations[2]=val2
但使用 jQuery 的 traditional parameterization 您将得到以下内容:
locations=[object+Object]
见
我有一个 key-value 对,我通过它发送如下值:
$(document).on('click', '.chkAll', function () {
$("#tableProducts thead tr th input:checkbox").change(function () {
$(".chkItem").prop('checked', $(this).prop("checked"));
if ($(this).prop('checked')) {
$('tbody tr td input[type="checkbox"]').each(function () {
product_ids[$(this).attr('id')] = "Checked";
productLocation[$(this).attr('id')] = $(this).attr('location');
});
} else {
$('tbody tr td input[type="checkbox"]').each(function () {
delete product_ids[$(this).attr('id')];
delete productLocation[$(this).attr('id')];
});
}
});
这就是我 post .NET MVC 操作中所有键的方式:
values: Object.keys(product_ids)
并在 Controller 中获取它:
public ActionResult GetValues(List<string> values)
{
// now I have all the keys here but now I'd like values as well...
}
我想象的方法是将整个 name-value 对数组直接传递到函数中,然后获取它,这样我就可以:
// key (product id) => 0124914815DD29
// value => checked
两个字符串 ...
我试过这样做:
values: product_ids // in jquery post
然后像这样获取值:
public ActionResult GetValues(Dictionary<string,string> values)
{
// but now the values dictionary is always empty
}
我该怎么做?
编辑:
这是数据所在的部分 posted:
$(".btnAnalyze").click(function () {
if (jQuery.isEmptyObject(product_ids) == true) {
ShowMessage("You haven't selected any product!");
return;
}
else {
var guid = '@Guid.NewGuid()'
var postData = { values: Object.keys(product_ids), drange: $('.range').val(), ID: guid, type: $('input[name=type]:checked').val(), shipping: $('input[name=shipping]:checked').val(), condition: $('input[name=condition]:checked').val(), minprice: $('.txtmin').val(), maxprice: $('.txtmax').val(), Title: $('.txtSearch').val(), negative: $('.txtNegativeKeywords').val(),locations: productLocation };
$.ajax({
type: "POST",
url: "/Analyze/GetAllProducts",
data: postData,
success: function (data) {
if (data == "Error") {
alert("Something went wrong");
} else if (data=="Ok"){
window.location.href = '/Analyze/Index/' + guid // redirect to another page
}
},
dataType: "json",
traditional: true
});
}
});
并映射 header 的动作:
[HttpPost]
public async Task<JsonResult> GetAllProducts(List<string> values, string drange, string ID, string type, string shipping, string condition, string minprice, string maxprice, string Title,string negative,string minFeedback, string maxFeedback, Dictionary<string,string> locations)
@Haim 请看我传递给 function 的最后一个参数,这就是我试图变成字典的参数...
显然,默认模型绑定器在尝试绑定 Dictionary
时需要以下格式:
locations[1]=val1&locations[2]=val2
但使用 jQuery 的 traditional parameterization 您将得到以下内容:
locations=[object+Object]
见