1

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.

adiga
  • 34,372
  • 9
  • 61
  • 83
Esmael
  • 69
  • 14
  • You need to give all the checkboxes the same `name` attribute and the name of the parameter would be the same - e.g. `` and in the method `string[] choices`. But you also need to give all your checkboxes a `value` attribute. The correct MVC way is to use a view model with a `bool` property - refer [this answer](https://stackoverflow.com/questions/29542107/pass-list-of-checkboxes-into-view-and-pull-out-ienumerable/29554416#29554416) for an example –  Jun 02 '17 at 23:17

2 Answers2

3

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)
{

}
MJ X
  • 8,506
  • 12
  • 74
  • 99
0

TBH it is better to use model for binding.

However, since you explicit say you don't want to then, this would be how to do it without specific model

in your razor view, give each checkbox same name (also same as the parameter name in your action) and a value

such that:

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

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

....

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

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

Then in your action:

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

}

if say user checked One, two and four, then in your answer parameter you will get U1, U2 and U4

Then base on the value in answer, you can determine each question's true or false

Alan Tsai
  • 2,465
  • 1
  • 13
  • 16
  • As I understood answer[0] is "U1", answer[1] is "U2",answer[3] is "U4" and answer[54] is "U55". Am I right? and for example answer[32] and the rest are null? – Esmael Jun 03 '17 at 18:38
  • @Esmael no - you only get checked values - so say if user checked U1 and U2, in the answer you only get two value (U1 and U2) - so any value you don't get means it was not checked. In essence, only **checked value** will show up in the answer array – Alan Tsai Jun 05 '17 at 01:35
  • I assume you also can answer to another question: https://stackoverflow.com/questions/44367863/passing-multi-parameters-radio-from-view-to-controller-using-array I appreciate your solution :) – Esmael Jun 05 '17 at 11:25