I need to send a dictionary of type <int,int>
to my controller via an Ajax post. The number of key value pairs in the post can range from 1 to 3, and may increase to 5 in the future (the exact values are not known at compile time).
In addition to the dictionary, I also have to include other data such as Id and name in the post.
How can I construct this dictionary in JavaScript, send it using JQuery post, and then receive it in the controller for processing?
Edit 2: I have decided to address this by sending a separate post request for each value instead of trying to pass a dictionary.
EDIT: Below is the source code for the function highlighting what I am attempting to achieve:
function BindAddMenuItem() {
$(".AddMenuItem").click(function (e) {
e.preventDefault();
// Retrieve header id from link by removing "AddMenuItem" from this.id
var currentId = $(this).attr("id").replace("AddMenuItem", "");
// Get itemnumber, itemname, itemdetails from textboxes with the same header id
var restaurantId = jQuery.trim($("#RestaurantId").val());
var itemNumber = jQuery.trim($("#ItemNumber" + currentId).val());
var itemName = jQuery.trim($("#ItemName" + currentId).val());
var itemDetails = jQuery.trim($("#ItemDetails" + currentId).val());
var costs = new Object();
// Select all textboxes with class "Header" + currentId
$(".Header" + currentId).each(function (i) {
var optionId = $(this).attr("id").replace("Option", "");
costs[optionId] = $(this).val();
});
$.ajax(
{
type: "POST",
url: "/Menu/AddMenuItem",
data: "reastaurantId=" + restaurantId + "&menuHeaderId=" + currentId + "&itemNumber=" + itemNumber + "&itemName=" + itemName + "&itemDetails=" + itemDetails + "&costs=" + costs,
dataType: "html",
success: function (result) {
var domElement = $(result);
$("#MenuContainer").replaceWith(domElement);
var newNum = parseInt(itemNumber) + 1;
$("#ItemNumber" + currentId).val(newNum);
BindAllBehaviours();
}
});
});
}