I am creating a registration screen where in a text field I provide a search and the search result will have to appear in a dropdownlist.
Theprojectisasp.netmvc4
c#andthecodeintheviewis:
<scripttype="text/javascript">
$(function () {
$("#searchBtn").click(function () {
obterInsumo($("#inputBusca").val());
});
});
function obterInsumo(insumo) {
$.getJSON('@Url.Action("ObterInsumo")?insumo=' + insumo, listaInsumoBack);
}
function listaInsumoBack(json) {
//AQUI É MINHA DÚVIDA...
<div class="form-group">
<div class="col-lg-2">
<label class="control-label input-sm">Buscar Insumo</label>
<div class="input-group">
<input type="text" class="form-control input-sm" id="inputBusca">
<span class="input-group-btn">
<button class="btn btn-transparente" type="button" id="searchBtn"><span class="glyphicon glyphicon-search"></span></button>
</span>
</div>
</div>
<div class="col-lg-2">
<label class="control-label input-sm">Insumo</label>
<select class="form-control input-sm" id="selectInsumo" name="selectInsumo" required>
<option selected="selected" value="">Efetue a busca</option>
</select>
</div>
</div>
On my Controller:
[Authorize]
public ActionResult ObterInsumo(string insumo)
{
InsumoService insumos = new InsumoService();
IQueryable<Insumo> i = insumos.ListarTodos().Where(x => x.Codigo.Contains(insumo)).OrderBy(x => x.InsumoId);
return Json(i, JsonRequestBehavior.AllowGet);
}
Through this method, I took the time to mount the dropdownlist
with the result of the search in the database. Is there a better way to do this? Or a light to effect the way I'm doing?