我有一个键值对,我通过它发送这样的值:
$(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')];
});
}
});
这就是我将所有密钥发布到.NET MVC Action中的方法:
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...
}
我想象这样做的方法是将整个名称 - 值对数组直接传递给函数,然后获取它,这样我就可以了:
// 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
}
我该怎么做?
编辑:
这是发布数据的部分:
$(".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
});
}
});
动作的映射标题:
[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请看我传入函数的最后一个参数,那是我试图变成字典的那个......
答案 0 :(得分:1)
显然,在尝试绑定Dictionary
时,默认模型绑定器需要以下格式:
locations[1]=val1&locations[2]=val2
但是使用jQuery的traditional parameterization代替你:
locations=[object+Object]