Hi mahesh213,
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
Model
public class CustomerModel
{
public string Id { get; set; }
public string Name { get; set; }
public List<SelectListItem> ItemCollection { get; set; }
}
Controller
public class HomeController : Controller
{
// GET: /Home/
public ActionResult Index()
{
CustomerModel model = new CustomerModel();
NorthwindEntities entities = new NorthwindEntities();
List<SelectListItem> items = new List<SelectListItem>();
foreach (Customer customer in entities.Customers)
{
items.Add(new SelectListItem { Text = customer.ContactName, Value = customer.CustomerID.ToString() });
}
model.ItemCollection = items;
return View(model);
}
[HttpPost]
public ActionResult NewRequest(CustomerModel model)
{
TempData["Customers"] = "Name : " + model.Name + "\\nID: " + model.Id;
return RedirectToAction("index");
}
}
View
<%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage<Searchable_DropDownList.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>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.2/css/bootstrap.min.css" />
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/chosen/1.8.7/chosen.min.css" />
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/chosen/1.8.7/chosen.jquery.min.js"></script>
<script type="text/javascript">
$(function () {
$('#ddlCustomers').chosen();
$("#ddlCustomers").on("change", function () {
$("#Id").val($(this).find('option:selected').val());
$("#Name").val($(this).find('option:selected').text());
});
});
</script>
</head>
<body>
<% using (Html.BeginForm("NewRequest", "Home", FormMethod.Post))
{%>
<%:Html.HiddenFor(model => model.Id) %>
<%:Html.HiddenFor(model => model.Name) %>
<%:Html.DropDownListFor(model => model.Id, new SelectList(Model.ItemCollection, "Value", "Text"), new { id = "ddlCustomers" })%>
<input type="submit" value="Get Selected Customer" />
<% if (TempData["Customers"] != null)
{%>
<script type="text/javascript">
window.onload = function () { alert('<%=TempData["Customers"] %>'); };
</script>
<% } %>
<% } %>
</body>
</html>
Screenshot