Hi nedash,
Check this example. Now please take its reference and correct your code.
Model
public class SearchViewModel
{
public int Id { get; set; }
public DataTable Ostans { get; set; }
}
Controller
public class HomeController : Controller
{
// GET: /Home/
public ActionResult Index()
{
var model = new SearchViewModel();
model.Ostans = PopulateOstan();
return View(model);
}
private DataTable PopulateOstan()
{
DataTable dt = new DataTable();
dt.Columns.AddRange(new DataColumn[3] { new DataColumn("Id", typeof(int)),
new DataColumn("Name", typeof(string)),
new DataColumn("Country",typeof(string)) });
dt.Rows.Add(1, "John Hammond", "United States");
dt.Rows.Add(2, "Mudassar Khan", "India");
dt.Rows.Add(3, "Suzanne Mathews", "France");
dt.Rows.Add(4, "Robert Schidner", "Russia");
return dt;
}
}
View
<%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage<_DataTable_ViewModel_MVC.Models.SearchViewModel>" %>
<%@ 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>
</head>
<body>
<%:Html.DropDownListFor(model => model.Ostans, (new SelectList(Model.Ostans.AsDataView(), "Id", "Name")))%>
</body>
</html>
Screenshot
