I have a Select2 that works well to search, the issue is that, once I select the value I was looking for, I need to fill the inputs (not dynamically created) with the information obtained. So far I have not found anything that makes reference to it, the code that I have is very simple.
$("#buscarCliente").select2({
placeholder: "Ingrese el nombre del cliente",
//Does the user have to enter any data before sending the ajax request
minimumInputLength: 0,
allowClear: true,
ajax: {
//How long the user has to pause their typing before sending the next request
delay: 150,
//The url of the json service
url: '@Url.Action("BuscarCliente", "Clientes")',
dataType: 'json',
async: true,
//Our search term and what page we are on
data: function (params) {
return {
pageSize: 100,
pageNum: params.page || 1,
searchTerm: params.term,
//Value from client side.
countyId: 'vv'
};
},
processResults: function (data, params) {
params.page = params.page || 1;
return {
results: $.map(data.Results, function (obj)
{
return { id: obj.IdCliente, text: obj.RUC + ' - ' + obj.Nombre };
}),
pagination: {
more: (params.page * 100) <= data.Total
}
};
},
cache: true,
formatResult: function (data) {
return data.Results.RUC
},
formatSelection: function (data) {
return obj;
},
escapeMarkup: function (m) { return m; },
dropdownCssClass: "bigdrop"
}
});
And my Controller
[HttpGet]
public ActionResult BuscarCliente(string searchTerm, int pageSize, int pageNum, string countyId)
{
var ResultQuery = context.Receptores.Where(m => m.Nombre.Contains(searchTerm)).ToList();
var itemList = new List<Select2Item>();
foreach (var Receptor in ResultQuery)
{
itemList.Add(new Select2Item
{
IdCliente = Cliente.Id,
Nombre= Cliente.Nombre,
RUC= Cliente.RUC,
Telefono = Cliente.Telefono,
Email = Cliente.Email
});
}
//Creating new object for return.
var result = new
{
Total = itemList.Count(),
Results = itemList.Skip((pageNum * pageSize) - 100).Take(pageSize)
};
//Return the data as a json result
return new JsonResult
{
Data = result,
JsonRequestBehavior = JsonRequestBehavior.AllowGet
};
}
How to assign the other values to existing inputs with Id?