<!doctype html>
@{
ViewBag.Title = "I2";
Layout = null;
}
<html lang="en">
<head>
<meta charset="utf-8">
<title>autocomplete demo</title>
<link href="~/Scripts/jquery-ui.css" rel="stylesheet">
<script src="~/Scripts/jquery-1.10.2.js"></script>
<script src="~/Scripts/jquery-ui.js"></script>
</head>
<body>
<label for="country">Select your country: </label>
<input id="country">
<script>
$("#country").autocomplete({
source: function (request, response) {
var item = new Array();
$.ajax({
async: false,
cache: false,
type: "POST",
url: "@(Url.Action("FindCountry", "Home"))",
data: { "term": request.term },
success: function (data) {
for (var i = 0; i < data.length ; i++) {
item[i] = { label: data[i].Value, Id: data[i].Key };
}
}
});
response(item);
},
});
</script>
</body>
</html>
and here is the function in the controller
[AcceptVerbs(HttpVerbs.Post)]
public JsonResult FindCountry(string term)
{
string cnnStr = ConfigurationManager.ConnectionStrings["GraphConnection"].ConnectionString;
SqlConnection dcnn = new SqlConnection(cnnStr);
SqlCommand dcmd = new SqlCommand("dbo.FindCountry", dcnn);
dcmd.CommandType = CommandType.StoredProcedure;
dcmd.Parameters.AddWithValue("@Term", term);
SqlDataAdapter da = new SqlDataAdapter();
da.SelectCommand = dcmd;
DataSet ds = new DataSet();
da.Fill(ds);
List<KeyValuePair<string, string>> cty = ds.Tables[0].AsEnumerable().Select(row => new KeyValuePair<string, string>((string)row["CountryCode"], (string)row["CountryName"])).ToList();
return Json(cty, JsonRequestBehavior.AllowGet);
}
No comments:
Post a Comment