Wednesday, May 6, 2009

Auto Complete In MVC.

1.Write Code in Aspx Page As Below:-


2.Add JavaScript As Below

 function SearchData()    //eVERY TIME IT GO TO DATABASE

{

//where Home IS Controller And Search Is Method,SearchValue IS argument

$.getJSON("/Home/Search",

{ SearchValue:document.getElementById("listSearch").value},

function(result)

{

$("#SearchResult").empty();

$.each(result, function(i,temp) {

$("#SearchResult").append("

");

});

});

}

  

var NameList;

$(document).ready(function()

{

//where Home IS Controller And Search Is Method,SearchValue IS argument

$.getJSON("/Home/Search",

{ SearchValue:""},

function(result)

{

NameList=result;

$.each(NameList, function(i,temp) {

$("#SearchResult2").append("

");

});

});

});

  

function Search1()   //IT DO AT LOCAL

{

$("#SearchResult2").empty();

var listSearch2=document.getElementById("listSearch2");

var txt=new RegExp(listSearch2.value,"i");

$.each(NameList, function(j,temp) {

if(temp.EmployeeName.search(txt)!=-1)

{

$("#SearchResult2").append("

");

}

});

}

 

3. Code In Controller Will Be like This:-

 public JsonResult Search(string SearchValue)

        {

            string EmpNameList = "";

            List<Employee> li = new List<Employee>();

            EmployeeDAL empDal = new EmployeeDAL();

            li = empDal.SearchEmployee(SearchValue);

            return Json(li);

        }

No comments: