Click here to Skip to main content
14,929,864 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
in view:
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
@using (Html.BeginForm()) {

@Html.TextBox("Search")
       <button type="submit" value="Search" id="btnSearch" name="Command">Search</button>
}
<script type="text/javascript">

C#
$(document).ready(function () {

        $("#Search").autocomplete({
            source: "/Search/Create",
            minLength: 1,
            select: function (event, ui) {
                if (ui.item) {
                    $("#Search").val(ui.item.value);
                    $("form").submit();
                }
            }
        });
    });
</script>



in controller:
SQL
public ActionResult Create(string Search)
       {

C#
var res = from m in db.Countries
                      where m.Countrys.Contains(Search)
                      select m;
            return Json(res, JsonRequestBehavior.AllowGet);
        }


but it not working can anyone tell me what is problem i am new to jquery and json
Posted

1 solution


View page



@Html.TextBox("Country")







@Styles.Render("~/Content/themes/base/css")


$(document).ready(function () {
$("#Country").autocomplete({
source: function(request,response) {
$.ajax({
url: "/Home/AutoCompleteCountry",
type: "POST",
dataType: "json",
data: { term: request.term },
success: function (data) {
response($.map(data, function (item) {
return { label: item.Country, value: item.Country };
}))

}
})
},
messages: {
noResults: "", results: ""
}
});
})


here is the code of AutoCompleteCountry method that will accept a parameter and return the matching JSON data.

public JsonResult AutoCompleteCountry(string term)
{
var result = (from r in db.Customers
where r.Country.ToLower().Contains(term.ToLower())
select new { r.Country }).Distinct();
return Json(result, JsonRequestBehavior.AllowGet);
}
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900