How do I pass a list of integers to an MVC action?

前端 未结 2 1469
猫巷女王i
猫巷女王i 2020-12-20 17:55

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

相关标签:
2条回答
  • 2020-12-20 18:33

    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.

    0 讨论(0)
  • 2020-12-20 18:40

    I dont think

    new List<int> {2, 2, 2}
    

    gets properly converted into an POST that is bindable, you want to send

    Ids=1&Ids=2&Ids=3
    

    a comma separate list may also work, im not sure, i don't use the crappy default modelbinder.

    Why are you doing that anyway? I hope thats pseudocode for something else...

    0 讨论(0)
提交回复
热议问题