Is there a way to pass this javascript object
Object { 35=true, 179=true, 181=true}
into a controller action as
Dictionary<int, bool>
I've checked the following methods:
var remoteUrl = "@Url.Action("UpdateProjectStructureSelection")" + "[email protected]";
$.post(remoteUrl, mapSiteSelectionChoices, function(callbackResult) {
alert(callbackResult);
});
and
var remoteUrl = "@Url.Action("UpdateProjectStructureSelection")" + "[email protected]";
$.post(remoteUrl, { values : mapSiteSelectionChoices }, function(callbackResult) {
alert(callbackResult);
});
However in both cases
public ActionResult UpdateProjectStructureSelection(int tlpId, Dictionary<int, bool> values)
has been called, but values was empty.
Since i've transfered more complex types into a controller action without writing a custom model binder i've been wondering whether i'm just doing something wrong here.
Is a custom model binder the only way here to get it as dictionary? (other than using JsonConvert + stringify on clientside)
Addition (This works but i'd love to avoid extra code) :
public ActionResult UpdateProjectStructureSelection(int tlpId, string values)
{
var dict = JsonConvert.DeserializeObject<Dictionary<int, bool>>(values);
}
See Question&Answers more detail:os