asp.net mvc - Live search MVC -
i'm looking live search asp.net , entity framework. i'm little bit green it. read needs use ajax, never used before , can't example. here piece of code, cshtml (part of textbox)
<div class="form-horizontal"> <hr /> <h4>search client: </h4> <div class="input-group"> <span class="input-group-addon" id="name"> <span class="glyphicon glyphicon-user" aria-hidden="true"></span> </span> @html.textbox("name", "", new { @class = "form-control", placeholder = "name" }) </div> <div><h6></h6></div> <div class="input-group"> <span class="input-group-addon" id="surname"> <span class="glyphicon glyphicon-user" aria-hidden="true"></span> </span> @html.textbox("surname", "", new { @class = "form-control", placeholder = "surname" }) </div> <div><h6></h6></div> <button type="submit" class="btn btn-default" data-toggle="modal" data-target="#infomodal">search</button> </div>
this part of controller:
public actionresult index(string name, string surname) { var searchlist = m in db.klienci select m; if (!string.isnullorempty(name)) { searchlist = searchlist.where(s => s.name.contains(name)); } if (!string.isnullorempty(surname)) { searchlist = searchlist.where(s => s.nazwisko.contains(surname)); } return view(searchlist); }
so search me clients name , surname, refresh full page when lost focus or after clicking button. how solve it, live search? after each keystroke search through database? i'm little bit green, me?
you can listen keyup
event on input element, read value , send server using ajax. return results , in ajax call's success callback, update ui results.
$(function() { $("#name,#surname").keyup(function(e) { var n = $("#name").val(); var sn = $("#surname").val(); $.get("/home/index?name="+n+"&surname="+sn,function(r){ //update ui results $("#resultstable").html(r); }); }); });
the code listens key event on 2 input textboxes , read values , send /home/index
action method using jquery method asynchronously.when action method returns response, update dom.
assuming resultstable
id of table list results.
also, since returning partial view result ( without layout headers), should use return partialview()
instead of return view()
if(request.isajaxrequest()) return partialview(searchlist); return view(searchlist);
Comments
Post a Comment