filmov
tv
Part 97 Implement autocomplete textbox functionality in mvc

Показать описание
Link for code samples used in the demo
Healthy diet is very important both for the body and mind. If you like Aarvi Kitchen recipes, please support by sharing, subscribing and liking our YouTube channel. Hope you can help.
Step 1: We will be using tblStudents table in this demo.
Step 5: Right click on the "Controllers" folder and add "Home" controller. Copy and paste the following code. Please make sure to include "MVCDemo.Models" namespace.
public class HomeController : Controller
{
public ActionResult Index()
{
DatabaseContext db = new DatabaseContext();
return View(db.Students);
}
[HttpPost]
public ActionResult Index(string searchTerm)
{
DatabaseContext db = new DatabaseContext();
List[Student] students;
if (string.IsNullOrEmpty(searchTerm))
{
students = db.Students.ToList();
}
else
{
students = db.Students
.Where(s =] s.Name.StartsWith(searchTerm)).ToList();
}
return View(students);
}
public JsonResult GetStudents(string term)
{
DatabaseContext db = new DatabaseContext();
List[string] students = db.Students.Where(s =] s.Name.StartsWith(term))
.Select(x =] x.Name).ToList();
return Json(students, JsonRequestBehavior.AllowGet);
}
}
Step 6: Right click on the "Index" action method in the "HomeController" and add "Index" view. Copy and paste the following code.
@model IEnumerable[MVCDemo.Models.Student]
[script type="text/javascript"]
$(function () {
$('#txtSearch').autocomplete({
minLength: 1
});
});
[/script]
[div style="font-family:Arial"]
{
[label for="Name"]Name: [/label]
@Html.TextBox("searchTerm", null, new { id = "txtSearch" })
[input type="submit" value="Search" /]
}
[table border="1"]
[tr]
[th]
@Html.DisplayNameFor(model =] model.Name)
[/th]
[th]
@Html.DisplayNameFor(model =] model.Gender)
[/th]
[th]
@Html.DisplayNameFor(model =] model.TotalMarks)
[/th]
[/tr]
@foreach (var item in Model)
{
[tr]
[td]
@Html.DisplayFor(modelItem =] item.Name)
[/td]
[td]
@Html.DisplayFor(modelItem =] item.Gender)
[/td]
[td]
@Html.DisplayFor(modelItem =] item.TotalMarks)
[/td]
[/tr]
}
[/table]
[/div]
Healthy diet is very important both for the body and mind. If you like Aarvi Kitchen recipes, please support by sharing, subscribing and liking our YouTube channel. Hope you can help.
Step 1: We will be using tblStudents table in this demo.
Step 5: Right click on the "Controllers" folder and add "Home" controller. Copy and paste the following code. Please make sure to include "MVCDemo.Models" namespace.
public class HomeController : Controller
{
public ActionResult Index()
{
DatabaseContext db = new DatabaseContext();
return View(db.Students);
}
[HttpPost]
public ActionResult Index(string searchTerm)
{
DatabaseContext db = new DatabaseContext();
List[Student] students;
if (string.IsNullOrEmpty(searchTerm))
{
students = db.Students.ToList();
}
else
{
students = db.Students
.Where(s =] s.Name.StartsWith(searchTerm)).ToList();
}
return View(students);
}
public JsonResult GetStudents(string term)
{
DatabaseContext db = new DatabaseContext();
List[string] students = db.Students.Where(s =] s.Name.StartsWith(term))
.Select(x =] x.Name).ToList();
return Json(students, JsonRequestBehavior.AllowGet);
}
}
Step 6: Right click on the "Index" action method in the "HomeController" and add "Index" view. Copy and paste the following code.
@model IEnumerable[MVCDemo.Models.Student]
[script type="text/javascript"]
$(function () {
$('#txtSearch').autocomplete({
minLength: 1
});
});
[/script]
[div style="font-family:Arial"]
{
[label for="Name"]Name: [/label]
@Html.TextBox("searchTerm", null, new { id = "txtSearch" })
[input type="submit" value="Search" /]
}
[table border="1"]
[tr]
[th]
@Html.DisplayNameFor(model =] model.Name)
[/th]
[th]
@Html.DisplayNameFor(model =] model.Gender)
[/th]
[th]
@Html.DisplayNameFor(model =] model.TotalMarks)
[/th]
[/tr]
@foreach (var item in Model)
{
[tr]
[td]
@Html.DisplayFor(modelItem =] item.Name)
[/td]
[td]
@Html.DisplayFor(modelItem =] item.Gender)
[/td]
[td]
@Html.DisplayFor(modelItem =] item.TotalMarks)
[/td]
[/tr]
}
[/table]
[/div]
Комментарии