Reputation: 752
I have the following jquery code.
<script src="Scripts/jquery-1.10.2.min.js"></script>
<script>
function FillOption() {
$.ajax({
type: "Post",
url: "/contratoBuscar/GetItem/",
dataType: 'json',
success: function (data) {
for (var i = 0; i < data.length; i++) {
$('#mydrop').append('<option value=' + data[i].Value + '>' + data[i].Text + '</option > ');
}
}
});
}
</script>
I would like to pass a string to my GetItem fonction from the follwing cshtml field
@Html.TextBox("selected_cliente_contrato", Model.selected_cliente_contrato, new { @id = "clienteContratoAutocomplete", @type = "text", @placeholder = "clientes", @class = "form-control" })
Here is the code for my controller [HttpPost] public ActionResult GetItem(string clienteName) {
contratoBuscarModel contrato = new contratoBuscarModel();
contrato.selected_cliente_contrato = clienteName;
contratoBuscarContext contratoBuscar = new contratoBuscarContext();
clienteContratosContext clienteContratosContext = new clienteContratosContext();
List<SelectListItem> drop = new List<SelectListItem>();
foreach (contratoModel contract in contratoBuscar.GetAllContratosFiltrar(contrato).contratosFound)
{
drop.Add(new SelectListItem
{
Text = contract.numero_contrato,
Value = contract.numero_contrato
});
}
return Json(drop);
}
My question is how can I get the clienteName from my htmlTextBox pass it in the jquery so I can use it in my actionResult. I have tried many times and my clienteName keeps being null. Thank you for your help.
Upvotes: 1
Views: 153
Reputation: 96
On your url line, change it to the following:
url: "/contratoBuscar/GetItem?clienteName=" + $("#clienteContratoAutocomplete").val()
primitive types for some reason do not work if passed in in data field so you have to pass them in in the url. Assuming default routes, since string clienteName
is not id
, you have to pass it like a get parameter ?varname=value
.
Upvotes: 2