4

I need to put the same NAME tag for Html.CheckBox somehow .

How I can do it? Here is my code... Thank you!

foreach (var answer in @question.Answers)
{
@Html.CheckBox("answer_CheckBox_" + answer.ID.ToString(), false, new { Value = answer.ID });  
<label style="margin-left: 0.5em;">@answer.Title</label>
<br />                                                                                                         
}

2 Answers 2

3

You're using an incrementing value as part of the name argument to the CheckBox() method (the first argument), so naturally they're going to be different names in the rendered HTML.

If you need them all to have the same name attribute value, use a static value:

@Html.CheckBox("answer_CheckBox", false, new { Value = answer.ID });
Sign up to request clarification or add additional context in comments.

Comments

2

Does this work?

@Html.CheckBox("answer", false, new { name="answer", value = answer.ID });

1 Comment

"answer_CheckBox_" + answer.ID.ToString() must have been taking over the name. Updated. Try again.

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.