I am using a model in MVC3 to populate textboxes on my page like so :
<input name="test" value="a" type="radio" id="emp_contributions_gbp" @if (Model.myvalue.ToString() == "x"){<text>checked=true</text>}>
This works perfectly fine for saying – “if model.myvalue = x, then check this box”
However, I want to be able to return this model to the controller so I can persist the data should it be updated.
I am now using:
@Html.TextBoxFor(m => m.someField) which works perfectly with the model, however I have no idea how I could use .CheckboxFor and my IF statements together
You can use the
CheckBoxFormethod like so:See here for the MSDN documentation of this overload.
Update
Consider using the
RadioButtonFormethod instead:Model:
View:
You can then preselect some radio by setting the view model property to the corresponding value: