0

I have this form in my view:

@using (Html.BeginForm()) { @Html.HiddenFor(o=>o.NoteId) for (int i = 0; i < Model.Friends.Count; i++) { <table> <tr> <td> <p style="font-size: 1.3em; color: black">@Model.Friends[i].Name</p> </td> <td> @Html.CheckBoxFor(x => @Model.Friends[i].Checked) </td> </tr> </table> } 

This displays a name with a checkbox next to it. When i post it to my controller it passes the values in the checkboxes but it does not post the names. Is it possible to just "display" tha names but still pass it to my controller on submit?

I tried something like this:

@Html.LabelFor(x => @Model.Friends[i].Name) 

But it only displays the property (name) in front of my checkbox. Any tips on how to achieve this?

1
  • 1
    Set a hidden field. Im not familiar with razor view engine, but a quick google suggests @Html.HiddenFor Commented Sep 14, 2014 at 20:43

1 Answer 1

1

In order to get the names back, you need to use something that will generate an input element so that it will be part of the form collection.

Using @Html.HiddenFor() will accomplish what you are looking for.

for (int i = 0; i < Model.Friends.Count; i++) { <table> <tr> <td> <p style="font-size: 1.3em; color: black">@Model.Friends[i].Name</p> </td> <td> @Html.CheckBoxFor(x => @Model.Friends[i].Checked) @Html.HiddenFor(x => @Model.Friends[i].Name </td> </tr> </table> } 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.