Description
In my previous related article I have explained how to use an autocomplete textbox using Jquery with a hardcoded value.
http://www.c-sharpcorner.com/UploadFile/b19d5a/autocomplete-textbox-in-razor-view-engine-using-jquery-in-as/
Here I will explain, when typing into a textbox, how the value will come from a database table.
In my application I have used the Entity Framework for database creation.
Now in my MVC3 application I have a model class named "student.cs" where I will store some student information.
public class Student
{
public int StudentID { get; set; }
public string LastName { get; set; }
public string FirstMidName { get; set; }
public DateTime EnrollmentDate { get; set; }
}
Also I have a controller class named "studentController.cs" under the controller folder.
Now for the studencontroller class I have an opening page in razor named "index.cshtml".
In this page I have a textbox where the user will enter the "firstname" of the student and it will give like autoextender features.
See the following code where I am catching the user data:
@using (Html.BeginForm())
{
<p>
Find by name: @Html.TextBox("SearchString")
<input type="submit" value="Search" /></p>
}
Now we have to handle the textbox enter event in our student controller class.
For that we have to write a particular ActionResult name "AutocompleteSuggestions" in our "studentController.cs" class.
See the following code:
public JsonResult AutocompleteSuggestions(string searchstring)
{
private SchoolContext db = new SchoolContext();
Join the conversation! Your thoughts help the community grow.
Sign in to leave a comment
It is the same account you read, post and publish with — and you will come straight back to this page.



anurag pathakPosted Dec 29, 2017, 3:59 AM
How to do that in repeater
surya sivadasPosted Dec 14, 2017, 3:27 AM
Have used the same approach but my text box does not display the data, but my query is having the data in it so pls provide the solution\
No NamePosted Jun 8, 2017, 2:56 AM
I wanted to say thank you very much for this tutorial!
farooq smdPosted Jun 21, 2016, 12:24 PM
nice one
Atik ShaikhPosted Apr 12, 2016, 4:40 AM
Have used the same approach but my text box does not display the data, but my query is having the data in it so pls provide the solution
Bhavesh PatelPosted Apr 30, 2015, 6:28 AM
Thank you
Nehal GaikwadPosted Nov 23, 2013, 7:00 AM
Thanks a lot...this really helped me....God bless u !!!!!
hesh hshPosted Jan 7, 2013, 10:58 AM
Hi I was wondering how I could group the lists of names on the JsonResult AutocompleteSuggestions function thanks Hesh
sagar jagPosted Nov 29, 2012, 12:51 AM
hi sir nice article but the problem is i m getting the values in my controller namelist variable but i m getting the output ,,plss help me out thank u
Rohit BassiPosted Nov 12, 2012, 12:23 PM
Thanks Shirsendu for such a useful article !
Renato CantarinoPosted Mar 12, 2012, 3:40 PM
public JsonResult AutocompleteSuggestions(string searchstring) // (string term) { code here } Action needs to map to the parameter 'term'.
Abhi KumarPosted Feb 3, 2012, 3:47 AM
Very useful article.
Sonakshi SinghPosted Feb 2, 2012, 10:56 PM
Very useful..thank you for posting
Monika AroraPosted Feb 2, 2012, 10:50 PM
Hi Shirsendu. You have presented your article very nicely.