0

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

1 Answer 1

1

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.

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

Comments

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.