Hello StackOverFlow colleagues, I have the following question, how can I load or give a value to an input when selecting an option in a select or dropdownlist.
my create controller is as follows:
// GET: Collaborators/Create
public ActionResult Create()
{
var lideres = (from p in db.Collaborators
where p.grupo_lider == "SI"
select p).ToList();
ViewBag.lideres = lideres.Select(p => new SelectListItem() { Value = p.nombres, Text = p.nombres }).ToList();
ViewBag.CompanyID = new SelectList(db.Companies, "CompanyID", "compania");
return View();
}
Part of my view:
<div class="form-group">
@Html.LabelFor(model => model.lider, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.DropDownListFor(model => model.lider, (List<SelectListItem>)ViewBag.lideres, "Selleccione un líder", new{ @class = "form-control" })
@Html.ValidationMessageFor(model => model.lider, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.cargo_lider, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.cargo_lider, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.cargo_lider, "", new { @class = "text-danger" })
</div>
</div>
Part of my model with the most relevant parts regarding my question:
[Display(Name = "Lider")]
public string lider { get; set; }
[Display(Name = "Cargo Lider")]
public string cargo_lider { get; set; }
[Display(Name = "Cargo")]
public string cargo { get; set; }
I explain exactly what I need, currently my dropdownList that has as a model model.lider
shows the names of the users whose group_leader is equal to "SI"
in the controller shows the query that I make, what I need now is to load the input @Html.EditorFor(model => model.cargo_lider, new { htmlAttributes = new { @class = "form-control" } })
with the position according to the name or the user that is selected in the dropdownlist, taking into account that this data is in cargo
the db.collaborators column, and I need to load it in the input with the modelo.cargo_lider
.
I stay tuned for questions, answers and others.
Current Ajax Code:
$('#iddropdownlist').change(function () {
var url = "/Collaborators/BuscarCargoLider?claveLider=" + $('#iddropdownlist option:selected').val();
$.ajax({
url: url,
type: 'GET',
dataType: 'json',
contentType: 'application/json; charset=utf-8',
async: true,
success: function (result) {
var lider;
if (result != null && result.length > 0) {
lider= JSON.parse(result);
}
if (lider!= null ) {
$('#idinput').val(lider);//cargo debe ser la propiedad de la clase lider donde se almacena el cargo del lider
}
},
error: function (XMLHttpRequest, textStatus, errorThrown) {
alert("error al consultar datos del lider");
}
});
});
Controller:
[HttpGet]
public JsonResult BuscarCargoLider(string claveLider)
{
try
{
var lider = (from p in db.Collaborators
where p.nombres == claveLider
select p.cargo).ToList();
return Json(lider, JsonRequestBehavior.AllowGet);
}
catch (Exception)
{
}
return Json("", JsonRequestBehavior.AllowGet);
}
in controller:
In view:
In the view:
To put an id to the dropdownlist and input