I am beginner to MVC. am trying to develop a search engine using Ajax. my project have two views and one controller.. search screen having a textbox and a search button.
if i enter the value in the text box and click search button it navigate to next page(search result page) and the textbox filled with value which we give in search screen page textbox.
(or)
Do you have any other idea in Search engine project example similar with my project .. kindly share with me..
Thanks..
Search Screen Page:
Search Result Page:
@model Tuple
@{
ViewBag.Title = "Get User";
}
Search Result
Keyword : @*textbox*@ @*search*@ @*AllSearch*@ |
@section Scripts{
$(document).ready(function () {
// This is for Get All Data
$("#btnAllUser").click(function () {
$.ajax({
url: "@Url.Action("GetAllUser","User")",
data: "",
type: "GET",
dataType: "json",
success: function (data) {
loadData(data);
},
error: function () {
alert("Failed! Please try again.");
}
});
});
// this will use for Get Data based on parameter
$("#btnSearch").click(function () {
$.ajax({
url: "@Url.Action("GetUserWithParameter","User")",
data: { prefix: $('#txtSearch').val() },
type: "GET",
dataType: "json",
success: function (data) {
loadData(data);
},
error: function () {
alert("Failed! Please try again.");
}
});
});
function loadData(data) {
// Here we will format & load/show data
var tab = $('
');
var thead = $('');
thead.append('Business Name ');
thead.append('Category ');
thead.append('Description ');
tab.append(thead);
$.each(data, function (i, val) {
// Append database data here
var trow = $(' ');
trow.append('' + val.BusinessName + ' ');
trow.append('' + val.BusinessCategory + ' ');
trow.append('' + val.BusinessDescription + ' ');
tab.append(trow);
});
$("#UpdatePanel").html(tab);
};
});
}
Controller:
public ActionResult GetUser(string keyword)
{
return View(new Tuple(keyword));
}
[HttpPost]
public ActionResult GetSearch(FormCollection frm, string search)
{
return RedirectToAction("GetUser", new { keyword = frm["search"] });
}
public JsonResult GetAllUser()
{
List allUser = new List();
// Here "MyDatabaseEntities " is dbContext, which is created at time of model creation.
using (MvcApplication1Entities2 dc = new MvcApplication1Entities2())
{
allUser = dc.tblBusinessCategories.ToList();
}
return new JsonResult { Data=allUser, JsonRequestBehavior = JsonRequestBehavior.AllowGet };
}
public JsonResult GetUserWithParameter(string prefix)
{
List allUser = new List();
// Here "MyDatabaseEntities " is dbContext, which is created at time of model creation.
using (MvcApplication1Entities2 dc = new MvcApplication1Entities2())
{
allUser = dc.tblBusinessCategories.Where(a => a.BusinessDescription.Contains(prefix) || a.BusinessName.Contains(prefix) || a.BusinessCategory.Contains(prefix)).ToList();
}
return new JsonResult { Data = allUser, JsonRequestBehavior = JsonRequestBehavior.AllowGet };
}
}
}
public ActionResult GetUser(string keyword)
{
return View(new Tuple
}
[HttpPost]
public ActionResult GetSearch(FormCollection frm, string search)
{
return RedirectToAction("GetUser", new { keyword = frm["search"] });
}
public JsonResult GetAllUser()
{
List
// Here "MyDatabaseEntities " is dbContext, which is created at time of model creation.
using (MvcApplication1Entities2 dc = new MvcApplication1Entities2())
{
allUser = dc.tblBusinessCategories.ToList();
}
return new JsonResult { Data=allUser, JsonRequestBehavior = JsonRequestBehavior.AllowGet };
}
public JsonResult GetUserWithParameter(string prefix)
{
List
// Here "MyDatabaseEntities " is dbContext, which is created at time of model creation.
using (MvcApplication1Entities2 dc = new MvcApplication1Entities2())
{
allUser = dc.tblBusinessCategories.Where(a => a.BusinessDescription.Contains(prefix) || a.BusinessName.Contains(prefix) || a.BusinessCategory.Contains(prefix)).ToList();
}
return new JsonResult { Data = allUser, JsonRequestBehavior = JsonRequestBehavior.AllowGet };
}
}
}
Replies
Know the answer? Post it — somebody with the same question will find it here.