Hi Lakhvir,
Check this example. Now please take its reference and correct your code.
Note: Get the customer records from Database. For example purpose i have used Temporary Data.
Model
public class CustomerModel
{
public int Id { get; set; }
public string Name { get; set; }
public string City { get; set; }
public string Country { get; set; }
}
Controller
public class HomeController : Controller
{
// GET: /Home/
public ActionResult Index()
{
BindDropDownList();
return View(Customers());
}
private void BindDropDownList()
{
List<SelectListItem> countries = new List<SelectListItem>();
countries.Add(new SelectListItem { Text = "Select", Value = "0" });
countries.Add(new SelectListItem { Text = "USA", Value = "USA" });
countries.Add(new SelectListItem { Text = "India", Value = "India" });
TempData["Countries"] = countries;
List<SelectListItem> cities = new List<SelectListItem>();
cities.Add(new SelectListItem { Text = "Select", Value = "0" });
cities.Add(new SelectListItem { Text = "London", Value = "London" });
cities.Add(new SelectListItem { Text = "Mumbai", Value = "Mumbai" });
cities.Add(new SelectListItem { Text = "Bangalore", Value = "Bangalore" });
TempData["Cities"] = cities;
}
[HttpPost]
public ActionResult Index(string country, string city)
{
List<CustomerModel> customers = Customers();
if (country != "0")
{
customers = customers.Where(x => x.Country == country).ToList();
}
if (city != "0")
{
customers = customers.Where(x => x.City == city).ToList();
}
BindDropDownList();
return View(customers);
}
private List<CustomerModel> Customers()
{
List<CustomerModel> customers = new List<CustomerModel>();
customers.Add(new CustomerModel { Id = 1, Name = "Rohit", City = "Mumbai", Country = "India" });
customers.Add(new CustomerModel { Id = 2, Name = "John", City = "London", Country = "USA" });
customers.Add(new CustomerModel { Id = 3, Name = "Smith", City = "Bangalore", Country = "India" });
customers.Add(new CustomerModel { Id = 4, Name = "Jayasuriya", City = "London", Country = "USA" });
customers.Add(new CustomerModel { Id = 5, Name = "Ching", City = "London", Country = "USA" });
return customers;
}
}
View
<%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage<IEnumerable<Multiple_DropDownList_Filter_MVC.Models.CustomerModel>>" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Index</title>
</head>
<body>
<div>
<%using (Html.BeginForm("Index", "Home", FormMethod.Post)) {%>
Country: <%:Html.DropDownList("country", (IEnumerable<SelectListItem>)TempData["Countries"])%>
City : <%:Html.DropDownList("city", (IEnumerable<SelectListItem>)TempData["Cities"])%>
<input type="submit" value="Search" />
<%} %>
<hr />
<table>
<tr>
<th>Id</th>
<th>Name</th>
<th>City</th>
<th>Country</th>
</tr>
<%foreach (var customer in Model)
{%>
<tr>
<td><%=customer.Id%>/td>
<td><%=customer.Name%></td>
<td><%=customer.City%></td>
<td><%=customer.Country%></td>
</tr>
<% } %>
</table>
</div>
</body>
</html>
Screenshot