I have below search code in MVC4, whose functioning is as searching a keyword from the database on the basis of text box and drop down selection...
All are working fine and display the result on the same page but I need to show the filtered searched data on to the next page.....
Below is my Model Class (EmployeeModel.cs)
public class EmployeeModel
{
public int ID { get; set; }
public string Emp_ID { get; set; }
public string Name { get; set; }
public string Dept { get; set; }
public string City { get; set; }
public string State { get; set; }
public string Country { get; set; }
public string Mobile { get; set; }
}
Below is my Controller (EmployeeController.cs)
public class EmployeeController : Controller
{
//
// GET: /Employee/
private EmployeeDataContext objEmpDataCont;
public EmployeeController()
{
objEmpDataCont = new EmployeeDataContext();
}
public ActionResult Index(string Search_Data, string Emp_Dept)
{
var DeptList = new List<string>();
var DeptQuery = from q in objEmpDataCont.Employees orderby q.Dept select q.Dept;
DeptList.AddRange(DeptQuery.Distinct());
ViewBag.Emp_Dept = new SelectList(DeptList);
IList<employeemodel> empList = new List<employeemodel>();
var emp = from q in objEmpDataCont.Employees
select q;
if (!String.IsNullOrEmpty(Search_Data))
{
emp = emp.Where(s => s.Name.Contains(Search_Data));
}
if (!String.IsNullOrEmpty(Emp_Dept))
{
emp = emp.Where(s => s.Dept == Emp_Dept);
}
var myEmpList = emp.ToList();
foreach (var empData in myEmpList)
{
empList.Add(new EmployeeModel()
{
ID = empData.ID,
Emp_ID = empData.Emp_ID,
Name = empData.Name,
Dept = empData.Dept,
City = empData.City,
State = empData.State,
Country = empData.Country,
Mobile = empData.Mobile
});
}
return View(empList);
}
Below is my View (Index.cshtml)
<%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage<IEnumerable<search.Models.EmployeeModel>>" %>
<!DOCTYPE html>
<html>
<head runat="server">
<meta name="viewport" content="width=device-width" />
<title>Index</title>
</head>
<body>
<p>
<% using (Html.BeginForm()) { %>
</p>
<p>
<% { %>
Type Name To Search: <%: Html.TextBox("Search_Data", ViewBag.FilterValue as string)%><br />
Employee Department <%: Html.DropDownList("Emp_Dept","All") %>
<input type="submit" value="Search" />
<% } %>
</p>
<table>
<tr>
<th>
<%: Html.DisplayNameFor(model => model.Emp_ID) %>
</th>
<th>
<%: Html.DisplayNameFor(model => model.Name) %>
</th>
<th>
<%: Html.DisplayNameFor(model => model.Dept) %>
</th>
<th>
<%: Html.DisplayNameFor(model => model.City) %>
</th>
<th>
<%: Html.DisplayNameFor(model => model.State) %>
</th>
<th>
<%: Html.DisplayNameFor(model => model.Country) %>
</th>
<th>
<%: Html.DisplayNameFor(model => model.Mobile) %>
</th>
<th></th>
</tr>
<% foreach (var item in Model) { %>
<tr>
<td>
<%: Html.DisplayFor(modelItem => item.Emp_ID) %>
</td>
<td>
<%: Html.DisplayFor(modelItem => item.Name) %>
</td>
<td>
<%: Html.DisplayFor(modelItem => item.Dept) %>
</td>
<td>
<%: Html.DisplayFor(modelItem => item.City) %>
</td>
<td>
<%: Html.DisplayFor(modelItem => item.State) %>
</td>
<td>
<%: Html.DisplayFor(modelItem => item.Country) %>
</td>
<td>
<%: Html.DisplayFor(modelItem => item.Mobile) %>
</td>
</tr>
<% } %>
<%} %>
</table>
</body>
</html>