通过字典控制器asp.net的MVC [英] pass dictionary to controller asp.net mvc
问题描述
我想通过类型的字典< INT,INT>
来通过Ajax后我控制器。
这里主要的原因是后可能有键值对1-3之间的位置(这些值都不被称为在编译时),并在将来有可能上升到5。
另外,在文章中,我要通过一些其他的数据,如ID和名称,这一切工作正常。
我将如何构建该辞典中的JavaScript,然后通过JQuery的帖子发送,并最终接受它的控制器来处理?
编辑2: 我已经决定只解决这个问题有一个职位的每个值,而不是试图通过一本字典。
编辑: 下面是我的源功能,所以你可以看到什么,我想:
函数BindAddMenuItem(){
$(AddMenuItem)。点击(函数(五){
即preventDefault();
//获取头标识的链接通过this.id删除addmenuitem
VAR currentId = $(本).attr(ID)取代(AddMenuItem,)。
//获取itemnumber,了itemname,从文本框具有相同的标题ID itemdetails
VAR restaurantId = jQuery.trim($(#RestaurantId)VAL());
变种itemNumber = jQuery.trim($(#ItemNumber+ currentId).VAL());
变种了itemname = jQuery.trim($(#了itemname+ currentId).VAL());
变种itemDetails = jQuery.trim($(#ItemDetails+ currentId).VAL());
VAR成本=新的对象();
//选取所有文本框带班头+ currentId
$(头+ currentId)。每个(函数(一){
。VAR optionId = $(本).attr(ID)取代(选项,);
费用[optionId] = $(本).VAL();
});
$阿贾克斯(
{
键入:POST,
网址:/菜单/ AddMenuItem
数据:reastaurantId =+ restaurantId +与& menuHeaderId =+ currentId +与& itemNumber =+ itemNumber +与&了itemname =+了itemname +与& itemDetails =+ itemDetails +与&成本= +成本,
数据类型:HTML,
成功:函数(结果){
VAR domElement = $(结果);
$(#MenuContainer中)replaceWith(domElement)。
VAR newNum = parseInt函数(itemNumber)+ 1;
$(#ItemNumber+ currentId).VAL(newNum);
BindAllBehaviours();
}
});
});
}
喜欢的东西(JavaScript的)
字典=新的对象();
字典['12'] = 5;
字典['13'] = 6;
快译通['1000'] = 21;
快译通['9'] = 13;
字典['13'] = 48;
.post的$('/ client.mvc / mypostaction /',{myDictionary:字典});
INT,INT> 的属性类型
您就可以使用词典&LT发布
字典
对象到控制器
的ActionResult MyPostAction(词典<字符串,INT> myDictionary)
这是笔者的code第二次编辑:
我下面的工作,有当词典<字符串,INT> kvPairs
。 < INT,INT>
不会毕竟上班
请您的文章,如:
VAR字典=新的对象();
字典['13'] = 9;
字典['14'] = 10;
字典['2'] = 5;
.post的$('controller.mvc /测试',{'kvPairs:快译通},功能(OBJ){$('#输出)HTML(obj.Count);});
I am wanting to pass a dictionary of type <int,int>
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.
Also in the post I have to pass in some other data, such as Id and name, which all works as normal.
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?
Edit 2: I have decided to just solve this with a post for each value instead of trying to pass a dictionary.
EDIT: Here is my source for the function so you can see what I am trying:
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 + "&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();
}
});
});
}
Something like (javascript)
dict = new Object();
dict['12'] = 5;
dict['13'] = 6;
dict['1000'] = 21;
dict['9'] = 13;
dict['13'] = 48;
$.post('/client.mvc/mypostaction/', { myDictionary: dict });
You can then post the dict
object to your controller using a Dictionary<int, int>
as property type.
ActionResult MyPostAction(Dictionary<string, int> myDictionary)
edit from author's code second time:
The following works for me, when having a Dictionary<string, int> kvPairs
. <int, int>
isn't going to work after all.
Make your post like:
var dict = new Object();
dict['13'] = 9;
dict['14'] = 10;
dict['2'] = 5;
$.post('controller.mvc/Test', { 'kvPairs': dict }, function(obj) { $('#output').html(obj.Count); });
这篇关于通过字典控制器asp.net的MVC的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!