Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.7k views
in Technique[技术] by (71.8m points)

asp.net mvc - How do I pass a list of integers to an MVC action?

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?

Adding more detail about the scenario:

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.

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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>

Here's the link from Haack's block.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...