Search code examples
c#asp.net-mvcrazorasp.net-mvc-5html.beginform

Passing multi parameters from view to controller using array


I would like to post between 55 and 100 items which could be false or true from view to my controller without using Model. This is my code in Razor View:

@using(Html.BeginForm( "User","User",FormMethod.Post,new{enctype="multipart/form-data"}))
{

    <input type="checkbox" name="U1">Choice one</input>
    <input type="checkbox" name="U2">Choice two</input>
    <input type="checkbox" name="U3">Choice three</input>
    <input type="checkbox" name="U4">Choice four</input>

....

<input type="checkbox" name="U55">Choice fifty five</input>

<button type="submit">Send</button>
                               }

Controller:

[HttpPost]
public async Task<ActionResult> User(Array....)
{
return view();}

How can I send all of parameters (checkboxes) using an array to my controller. I appriciate all your solutions.


Solution

  • Dynamically you can do it like bellow code:

    @using(Html.BeginForm( "User","User",FormMethod.Post,new{enctype="multipart/form-data"}))
    {
    
        <input type="checkbox" name="answer[]" value="@dynamicValue">@dynamicValue</input>
    
        <button type="submit">Send</button>
    }
    

    and

    [HttpPost]
    public async Task<ActionResult> User(string[] answer)
    {
    
    }