5
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.
The project is asp.net mvc4
c# and the code in the view is:
<script type="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>
In 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 stopped at the time to assemble the dropdownlist
with the result of the database search. Is there a better way to do this? Or a light to effect the way I’m doing?
It worked perfectly @Harry Potter! I had done without being with Ilist and it didn’t work. It helped me a lot, thank you!
– RafaelMoura