Question

Good afternoon I have the following problem, I need to pass the values ​​that brings a CONTROLLER ViewBag a Checkbox in a view.

<table>
      <tr>
          <td>
              <label>Select:</label>
          </td>
          <td>
              <select id="type" name="type" multiple="multiple">
                <option id="Custom1" value="Custom1">Custom1</option>
                <option id="Custom2" value="Custom2">Custom2</option>
              </select>
          </td>
      </tr>
</table>

Normally in a (Input type="text") i put:

<input type="text" name="email" id="email" value="@(ViewBag.Example.EMail)" />

but in this case i dont know where put the value, could you help me please? Thanks

Was it helpful?

Solution

Something like this should do the trick, if the ViewBag item is a boolean:

<option id="Custom1" value="Custom1" @(ViewBag.SomeBoolean ? "selected" : string.Empty)>Custom1</option>

It's worth noting, however, that this is very often more readily accomplished by binding the view to a model and setting that property on the model. Then you can make use of the HTML helpers, such as @Html.CheckBoxFor() and @Html.CheckBox(). The code you're showing indicates that you probably have logic in a controller which belongs in a model.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top