I want to search result from db andy show on same view using ajax but it is not happening. Here is my Index page where I defined the Search box and a table to show search result. Index View is
@{
Layout = null;
}
@model IEnumerable<BR.Models.PostJob>
@using (Ajax.BeginForm("AjaxSearch", "Student",
new AjaxOptions { HttpMethod = "GET", InsertionMode = InsertionMode.Replace, UpdateTargetId = "searchResults" }))
{
<input type="text" name="q" />
<input type="submit" value="Search" />
}
My Table to show Searched result is.
<table id="searchResults">
</table>
My Controller Function is.
public PartialViewResult AjaxSearch(string q)
{
SqlDataReader dr;
SqlConnection con = new SqlConnection("Data Source=IT-INTERN3;Initial Catalog=bridging_the_gap;Integrated Security=True");
SqlCommand cmd = new SqlCommand();
con.Open();
cmd.CommandText = "select * from Jobs where job_title ='" + q + "'";
cmd.Connection = con;
var r = cmd.ExecuteReader();
return this.PartialView(r);
}
My Partial View is
@model IEnumerable<BR.Models.PostJob>
<table>
<tr>
<th>
id
</th>
<th>
job_title
</th>
<th>
job_description
</th>
<th></th>
</tr>
@foreach (var item in Model) {
<tr>
<td>
@item.id
</td>
<td>
@item.job_title
</td>
<td>
item.job_description
</td>
</tr>
}
</table>
On click of search button it is going to AjaxSearch function but not showing result in Index view.