0
How to get the selected item from the Dropdownlist plugin Select2 (https://plugins.jquery.com/select2/), and send to a Controler via an Submit button ?
CONTROLER
public ActionResult Index(ContratoViewModel contratoViewModel)
{
int id = contratoViewModel.Id;
}
BUTTON THAT SUBMITS
<input class="btn btn-primary" value="Buscar" type="submit" id="btnBuscar" />
HTML
@using(Html.BeginForm("Index", "Contrato", FormMethod.Get))
{
<div class="row">
<div class="col-xs-3">
<label>Cliente</label><br />
<select id="ClienteID" name="ClienteID" class="form-control">
<option value="0">[SELECIONE]</option>
</select>
</div>
<div class="col-xs-3">
@Html.LabelFor(x => x.ContratoViewModel.NumeroContrato)
@Html.TextBoxFor(x => x.ContratoViewModel.NumeroContrato, new { @class = "form-control" })
</div>
<div class="col-xs-3">
@Html.LabelFor(x => x.ContratoViewModel.Responsavel)
@Html.TextBoxFor(x => x.ContratoViewModel.Responsavel, new { @class = "form-control" })
</div>
<div class="col-xs-2">
@Html.LabelFor(x => x.ContratoViewModel.Status)<br />
@Html.RadioButtonFor(x => x.ContratoViewModel.Status, "true", new { @checked = "checked" }) Ativo
@Html.RadioButtonFor(x => x.ContratoViewModel.Status, "false") Inativo
</div>
}
JAVASCRIPT
<script src="~/Content/Scripts/select2.full.min.js"></script>
<script type="text/javascript">
$(document).ready(function () {
$("#ClienteID").select2({
allowClear: true,
ajax: {
type: 'GET',
url: '/Contrato/ListarClienteJSON',
data: function (params) {
return {
pNome: params.term
};
},
dataType: 'json',
processResults: function (data) {
return {
results: data
};
}
}
});
});
</script>