发送javascript的对象控制器动作作为字典 [英] Transmit javascript object into controller action as dictionary
问题描述
有没有办法通过这个JavaScript对象
对象{35 = TRUE,179 = TRUE,181 =真}
到一个控制器行动
词典< INT,BOOL>
我已经检查了以下方法:
VAR remoteUrl =@ Url.Action(UpdateProjectStructureSelection)+?tlpId=@Model.TopLevelProjectId
$。员额(remoteUrl,mapSiteSelectionChoices,功能(callbackResult){
警报(callbackResult);
});
和
VAR remoteUrl =@ Url.Action(UpdateProjectStructureSelection)+?tlpId=@Model.TopLevelProjectId
$。员额(remoteUrl,{值:mapSiteSelectionChoices},功能(callbackResult){
警报(callbackResult);
});
然而,在这两种情况下
公众的ActionResult UpdateProjectStructureSelection(INT tlpId,字典< INT,BOOL>数值)
被调用,但值是空的。
因为我已经转移更复杂的类型到一个控制器的动作,而无需编写自定义的模型绑定我一直在想,我是否只是在这里做得不对。
是自定义模型绑定这里唯一的方式得到它作为字典? (不是使用JsonConvert +字符串化的客户方等)
增加(这工作,但我很想避免额外的code):
公众的ActionResult UpdateProjectStructureSelection(INT tlpId,字符串值)
{
VAR字典= JsonConvert.DeserializeObject<&字典LT; INT,BOOL>>(值);
}
不与该格式的对象。这将需要在以下格式由所使用的 DefaultModelBinder
VAR数据= {'字典[0] .KEY':'35','字典[0] .value的':'真','字典[1]。关键 :'179','值[1] .value的':'真',字典[0] .KEY':'8','字典[0] .value的':'真'};
$。员额(remoteUrl,数据功能(callbackResult){
和所述控制器
公众的ActionResult UpdateProjectStructureSelection(词典< INT,BOOL>字典)
正如你提到的,另一种选择是使用自定义的 ModelBinder的
,例如<一个href=\"http://stackoverflow.com/questions/1077481/how-do-i-pass-a-dictionary-as-a-parameter-to-an-actionresult-method-from-jquery\">this回答
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")" + "?tlpId=@Model.TopLevelProjectId";
$.post(remoteUrl, mapSiteSelectionChoices, function(callbackResult) {
alert(callbackResult);
});
and
var remoteUrl = "@Url.Action("UpdateProjectStructureSelection")" + "?tlpId=@Model.TopLevelProjectId";
$.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);
}
Not with the object in that format. It would need to be in the following format to be used by the DefaultModelBinder
var data = { 'dict[0].key': '35', 'dict[0].value': 'True', 'dict[1].key': '179', 'values[1].value': 'True', dict[0].key': '8', 'dict[0].value': 'True' };
$.post(remoteUrl, data, function(callbackResult) {
and the controller
public ActionResult UpdateProjectStructureSelection(Dictionary<int, bool> dict)
As you noted, another option is to use a custom ModelBinder
, for example this answer
这篇关于发送javascript的对象控制器动作作为字典的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!