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.

1
  • You need to give all the checkboxes the same name attribute and the name of the parameter would be the same - e.g. <input name="choices" ... /> 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 for an example Commented Jun 2, 2017 at 23:17

2 Answers 2

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

}
Sign up to request clarification or add additional context in comments.

Comments

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

3 Comments

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 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
I assume you also can answer to another question: stackoverflow.com/questions/44367863/… I appreciate your solution :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.