// Model[Display(Name = "Servicio")][UIHint("DropDown")]public int ServicioId { get; set; }
// Controller// Data on InsertViewBag.CirujanoId = new SelectList(Enumerable.Empty<SelectListItem>());
// Data on EditViewBag.CirujanoId = new SelectList( new List<SelectListItem> { new SelectListItem { Selected = true, Text = dto.CirujanoNombre, Value = dto.CirujanoId.ToString() } }, "Value", "Text");
//Controller to fill Selectpublic async Task<JsonResult> JsFindServicio(string f){ var regs = from p in db.Servicios select p; if (f != null) { regs = regs.Where(x => x.Nombre.Contains(f)); } var dto = (await regs.ToListAsync()) .Select(s => new DTOSelect2 { id = s.ServicioId.ToString(), text = s.Nombre, html = "<div style=\"font-weight: bold;\">" + s.Nombre + "</div>" }); return Json(new { success = true, data = dto }, JsonRequestBehavior.AllowGet);}
public JsonResult JsGetMaterialesByFilter(string f, string tipo = "AC") { Object res = null; var result = from r in db.Materiales select r;
//var result = new List<DTOAutocompleteItem>(); if (!string.IsNullOrEmpty(f)) result = result.Where(x => x.Codigo.Contains(f) || x.Descripcion.Contains(f));
result = result.Take(autoCompleteSize);
if (tipo == "AC") { res = result.Select(p => new DTOAutocompleteItem { id = p.Codigo, text = p.Descripcion }) .ToList(); } if (tipo == "S") { res = result.Select(s => new DTOSelect2 { id = s.Codigo.ToString(), text = s.Descripcion, html = "<div style=\"font-weight: bold;\">" + s.Codigo + "</div><div style=\"font-size: 0.75em;\">" + s.Descripcion + "</div>" }) .ToList(); } return Json(new { data = res }, JsonRequestBehavior.AllowGet); }
// Razor// form<div class="col-md-4">@Html.EditorFor(x => x.ServicioId, new { optionsList = ViewBag.ServicioId })</div>
// bindFunctions$('#CirujanoId').select2({ theme: "bootstrap-5", dropdownParent: $('#form-body-child'), placeholder: "Seleccionar Cirujano", language: "es", ajax: { url: urls.FindCirujanos, dataType: 'json', data: function (p) { return { f: p.term, }; }, processResults: function (r) { return { results: $.map(r.data, function (item) { return { text: item.text.trim(), id: item.id, html: item.html } }) }; } },, // escapeMarkup: function (markup) { // return markup; // }, // templateResult: function (data) { // return data.html; // }, // templateSelection: function (data) { // $('#ProveedorId').val(data.id); // return data.text; // }, });