I have the following HTML code:
<div class="form-row">
<div class="form-group col-md-12">
<label class="form-label">Tipo de dirección</label>
<div class="form-inline">
<label class="custom-control custom-radio">
<input name="radio-direccion" value="local" type="radio" checked="checked" class="custom-control-input">
<span class="custom-control-label">Local </span>
</label>
<label class="custom-control custom-radio">
<input name="radio-direccion" value="foranea" type="radio" class="custom-control-input">
<span class="custom-control-label">Foránea</span>
</label>
</div>
</div>
</div>
<script type="text/javascript">
window._getTipoDirUrl = '@Url.Action("Action", "Controller")';
</script>
Also a ViewModel class that among other properties contains this:
public class DireccionViewModel
{
...
[Display(Name = "Tipo dirección")]
public string TipoDir {get; set;}
...
}
And a javascript code where I made an action based on the selected radio button:
$("[name=radio-direccion]").on('change', function () {
var $radio = $(this);
$.ajax({
url: window._getTipoDirUrl,
data: { tipodir: $radio.val() },
success: function (data) {
$("#direccion").html(data);
}
});
});
I need to represent the radio inputs with @ Html.CheckBoxFor () so that when the binding is done the property TypeDir is valid local or foreign (or some similar way so that in the controller I know which option has been selected) and also that the javascript code works correctly.