neda Derakhshesh neda Derakhshesh - 4 months ago 49
Javascript Question

implement Autocomplete Search box _ASP.net MVC

I searched a lot to find a plugin or a complete step by step tutorial to implement

autocomplete
search box
something like this

enter image description here

this is my model

public class Question
{
public virtual int Id { get; set; }
public virtual string Qu { get; set; }
public virtual string Ans { get; set; }
public virtual List<KeyWord> KeyWord { get; set; }
}


I want to find any related question in database when user enters it's text.

controller

[HttpPost]
public JsonResult AjaxQuestion()
{
return Json(db.Questions.ToList());
}


Appreciate if some one suggest me any way to implement this

Answer

You can use Jquery for recovering Data coming from controller (By Ajax), then you can use autocomplete method of Jquery as well...

$(document).ready(function () {
        $("#your_txt_box").autocomplete({
            source: function (request,response) {
                $.ajax({
                    url: 'controller',
                    dataType: 'json',
                    method: 'POST',
                    success: function (data) {
                        response($.map(data, function (item) {
                            return { field1: item.field1, field2: item.field2, field3: item.field3, field4: item.field4};
                        }))
                    }
                });
            }
        });
    });