如何将整数列表传递给 MVC 操作? [英] How do I pass a list of integers to an MVC action?

查看:25
本文介绍了如何将整数列表传递给 MVC 操作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个依赖于整数列表的操作.我的第一反应是简单地用 List 声明动作.

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)

在我的 View 调用中,如下所示:

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,它只呈现每本书的部分(我的这个视图的模型是 List),然后添加提交按钮.

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.

推荐答案

是的.默认模型绑定器可以将id"绑定到任何 ICollection.但是您必须提交多个具有相同名称的参数.这消除了使用辅助方法ActionLink".您可以使用 url helper Action 并将 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天全站免登陆