Note that there are some explanatory texts on larger screens.

plurals
  1. POpass dictionary to controller asp.net mvc
    primarykey
    data
    text
    <p>I am wanting to pass a dictionary of type <code>&lt;int,int&gt;</code> to my controller via an Ajax post. The main reason here is the post may have between 1-3 key value pairs here (none of these values are known at compile time) and in the future it may go up to 5.</p> <p>Also in the post I have to pass in some other data, such as Id and name, which all works as normal.</p> <p>How would I construct this dictionay in the javascript then send it via the JQuery post and finally receive it on the controller to process?</p> <p>Edit 2: I have decided to just solve this with a post for each value instead of trying to pass a dictionary.</p> <p>EDIT: Here is my source for the function so you can see what I am trying:</p> <pre><code>function BindAddMenuItem() { $(".AddMenuItem").click(function (e) { e.preventDefault(); //get header id from link by removing addmenuitem from this.id var currentId = $(this).attr("id").replace("AddMenuItem", ""); //get itemnumber, itemname, itemdetails from textboxes with 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 + "&amp;menuHeaderId=" + currentId + "&amp;itemNumber=" + itemNumber + "&amp;itemName=" + itemName + "&amp;itemDetails=" + itemDetails + "&amp;costs=" + costs, dataType: "html", success: function (result) { var domElement = $(result); $("#MenuContainer").replaceWith(domElement); var newNum = parseInt(itemNumber) + 1; $("#ItemNumber" + currentId).val(newNum); BindAllBehaviours(); } }); }); } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload