Hi AliYilmaz,
Check this example. Now please take its reference and correct your code.
Database
For this example I have used of Northwind database that you can download using the link given below.
Download Northwind Database
I have used SplitString in the query. For SplitString function you can refer below article.
Controller
public class HomeController : Controller
{
// GET: /Home/
public ActionResult Index()
{
List<SelectListItem> country = GetCountries();
TempData["Country"] = GetCountries();
DataTable dt = new DataTable();
string strConString = @"Data Source=.;Initial Catalog=Northwind;User ID=sa;password=123;";
using (SqlConnection con = new SqlConnection(strConString))
{
con.Open();
SqlCommand cmd = new SqlCommand("Select TOP 10 CustomerID,ContactName,City,Country from Customers", con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(dt);
}
return View(dt);
}
[HttpPost]
public ActionResult Index(string hfSelected)
{
DataTable dt = GetAllParameters(hfSelected.Split(',')); GetCountries();
TempData["Country"] = GetCountries();
return View(dt);
}
private static List<SelectListItem> GetCountries()
{
List<SelectListItem> country = new List<SelectListItem>();
country.Add(new SelectListItem { Text = "Argentina", Value = "Argentina" });
country.Add(new SelectListItem { Text = "Austria", Value = "Austria" });
country.Add(new SelectListItem { Text = "Belgium", Value = "Belgium" });
country.Add(new SelectListItem { Text = "Brazil", Value = "Brazil" });
return country;
}
public DataTable GetAllParameters(string[] sorumlu)
{
DataTable dt = new DataTable();
string strConString = @"Data Source=.;Initial Catalog=Northwind;User ID=sa;password=123;";
using (SqlConnection con = new SqlConnection(strConString))
{
con.Open();
SqlCommand cmd = new SqlCommand("Select CustomerID,ContactName,City,Country from Customers where Country in (SELECT Item FROM dbo.SplitString(@sorumlu, ','))", con);
string j = string.Join(",", sorumlu);
cmd.Parameters.AddWithValue("@sorumlu", j);
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(dt);
}
return dt;
}
}
View
<%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage<DataTable>" %>
<%@ Import Namespace="System.Data" %>
<!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>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.3/css/select2.min.css" />
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.3/js/select2.min.js"></script>
<script type="text/javascript">
$(function () {
$("#Country").select2({ placeholder: "Select", allowClear: false });
$('#Country').on('change', function (e) {
$('#hfSelected').val($(this).val());
});
});
</script>
</head>
<body>
<div>
<%Html.BeginForm("Index", "Home", FormMethod.Post);
{%>
<%:Html.Hidden("hfSelected")%>
<%:Html.DropDownList("Country", (IEnumerable<SelectListItem>)TempData["Country"], "Select Country", new { multiple = "multiple" })%>
<input type="submit" value="Save" />
<%}%>
<br />
<br />
<table>
<tr>
<th>ID</th>
<th>Name</th>
<th>City</th>
<th>Country</th>
</tr>
<% foreach (DataRow row in Model.Rows) {%>
<tr>
<td><%=row["CustomerId"] %></td>
<td><%=row["ContactName"] %></td>
<td><%=row["City"] %></td>
<td><%=row["Country"] %></td>
</tr>
<% }%>
</table>
</div>
</body>
</html>
Screenshot