我如何通过整数列表一个MVC行动? [英] How do I pass a list of integers to an MVC action?

查看:147
本文介绍了我如何通过整数列表一个MVC行动?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有依赖的整数列表上的操作。我的第一反应是简单地宣布用列表的操作。

I have an action that depends on a list of integers. My first instinct was to simply declare the action with a List.

我想声明的控制器的操作:

I tried declaring the action in the controller as:

public ActionResult EditMultiple(List<int> ids)

在我看来,通话就像这样:

and in my View call like so:

<%= Html.ActionLink("EditMultiple", "EditMultiple", new { ids = new List<int> {2, 2, 2} })%>

虽然编译列表,当我把一个断点在动作是空的。任何人都知道为什么还是有另一种方法?

Although it compiles the List is empty when I put a breakpoint in the action. Anybody know why or have an alternate approach?

添加详细了解情况:

我试图编辑在同一时间多个实体。我已经在那里我有一个应用程序,允许我创建有关在图书馆的书籍/编辑/查看的信息点。我有一个局部视图,允许用户编辑一本图书的信息,并将其保存到数据库中。

I'm trying to "Edit" multiple entities at the same time. I'm already at the point where I have an application that allows me to create/edit/view information about books in a library. I have a partial view that allows the user to edit information about a single book and save it to the database.

现在我想创建一个视图,它允许用户用一个提交按钮编辑有关多本图书的信息。我创建了一个动作EditMultiple刚刚呈现的部分为每本书(我对这一观点型号列表),并增加了提交按钮之后。

Now I'd like to create a View which allows the user to edit the information about multiple books with a single submit button. I've created an action EditMultiple which just renders the partial for each book (my model for this view is List) and adds the submit button afterwards.

推荐答案

是的。默认的模型粘合剂可以IDS绑定到任何ICollection的。但你必须使用相同的名称提交多个参数。这消除了使用辅助方法ActionLink的。您可以使用网址帮手行动并追加ID来链接,像这样:

Yes. The default model binder can bind "ids" to any ICollection. But you have to submit multiple parameters with the same name. That eliminates using the helper method "ActionLink". You can use url helper Action and append the ids to the link like so:

<a href="<%= Url.Action("CreateMultiple")%>?ids=2&ids=1&ids=3">Test link</a>

下面是来自链接哈克的区块。

这篇关于我如何通过整数列表一个MVC行动?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆