如何通过从JavaScript选定对象的名单在MVC的行动? [英] How to pass a list of selected objects from javascript in an action in MVC?
问题描述
我有一个包含各行的复选框的大型数据表。我需要为pass被选择为一个逗号分隔的列表,或更好,但一个强类型的IList到控制器的所有项目。什么是实现这一目标的最佳途径?我知道我可以传递一个字符串,没有问题,但我如何才能选择哪些复选框?
下面是静态code样子:
@using(@ Html.BeginForm(为removeItems,ResultList,新{= IDList表1,2,3,4},FormMethod.Post))
{
<输入类型=提交值=删除项目NAME =为removeItems/>
}
您可以像下面这样做使用jQuery。
$阿贾克斯({
网址:/ MyAction /阵列
数据:JSON.stringify({模式:输入})
键入:POST,
的contentType:应用/ JSON
});
下面输入是你的阵列,模式 - 行动参数的名称。
请确保您指定的contentType为application / JSON。
I have a large data table that contains a checkbox for each row. I need to either pass all items that are selected as either a comma delimited list, or better yet a strongly typed IList to the controller. What is the best way to accomplish this? I know I can pass a string with no issue, but how do I get which checkboxes are selected?
Here's what the static code looks like:
@using (@Html.BeginForm("RemoveItems", "ResultList", new { IDList = "1,2,3,4" }, FormMethod.Post))
{
<input type="submit" value="Remove Items" name="RemoveItems" />
}
You can do it like this using jQuery.
$.ajax({
url: "/MyAction/Array",
data: JSON.stringify({ model: input }),
type: "POST",
contentType: "application/json"
});
Here "input" is your array, "model" - name of action parameter. Make sure you've specified contentType as application/json.
这篇关于如何通过从JavaScript选定对象的名单在MVC的行动?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!