OK i got the Solution.
public JsonResult AutoCompleteCity(string term)
{
var obj = obj_db.Database.SqlQuery<Branch>("exec spGetCity @term={0}", term).
Select(x => new Branch {CityID=x.CityID,City=x.City,State=x.State,StateID=x.StateID,Region=x.Region,RegionID=x.RegionID,Country=x.Country,CountryID=x.CountryID }).ToList();
var listResult = new List<KeyValuePair<string, string>>();
foreach (var item in obj)
{
listResult.Add(new KeyValuePair<string, string>(item.CityID.ToString(), item.City));
}
var finalResult = listResult.Where(s => s.Value.ToLower().Contains
(term.ToLower())).Select(w => w).ToList();
return Json(finalResult, JsonRequestBehavior.AllowGet);
}
public JsonResult AutoCompleteState(string term)
{
var obj = obj_db.Database.SqlQuery<Branch>("exec spGetState @CityID={0}", Convert.ToInt32(term)).
Select(x => new Branch { CityID = x.CityID, City = x.City, State = x.State, StateID = x.StateID, Region = x.Region, RegionID = x.RegionID, Country = x.Country, CountryID = x.CountryID }).ToList();
var listResultExtra = new List<KeyValuePair<string, string>>();
var listResultRegion = new List<KeyValuePair<string, string>>();
var listResultCountry = new List<KeyValuePair<string, string>>();
foreach (var item in obj)
{
listResultExtra.Add(new KeyValuePair<string, string>(item.StateID.ToString(), item.State));
listResultExtra.Add(new KeyValuePair<string, string>(item.RegionID.ToString(), item.Region));
listResultExtra.Add(new KeyValuePair<string, string>(item.CountryID.ToString(), item.Country));
}
var finalResult = listResultExtra.Select(w => w).ToList();
return Json(finalResult, JsonRequestBehavior.AllowGet);
}
And the Script goes as:
<script type="text/javascript">
$("#BranchDetails_City").autocomplete({
source: function (request, response) {
var city = new Array();
$.ajax({
async: false,
cache: false,
type: "POST",
url: 'AutoCompleteCity',
data: { "term": request.term },
success: function (data) {
for (var i = 0; i < data.length ; i++) {
city[i] = { label: data[i].Value, Id: data[i].Key };
}
}
});
response(city);
},
select: function (event, ui) {
$.ajax({
async: false,
cache: false,
type: "POST",
url: 'AutoCompleteState',
data: { "term": ui.item.Id },
success: function (data) {
$("#BranchDetails_State").val(data[0].Value);
$("#hdfState").val(data[0].Key);
$("#BranchDetails_Region").val(data[1].Value);
$("#hdfRegion").val(data[1].Key);
$("#BranchDetails_Country").val(data[2].Value);
$("#hdfCountry").val(data[2].Key);
}
});
}
});
</script>