nedash says:
@Html.DropDownListFor(model => model.Reseller.OstanName, new SelectList((List<
SelectListItem
>)Model.Ostans,"Id","Name"),"please Select")
Since you are binding the SelectListItem to the Dripdownlist, SelectListItem doesn't contain the property as Id and Name. It contains Text, Value and Selected as the properties.
So you need to change the id with Value and name with Text.
@Html.DropDownListFor(model => model.Reseller.OstanName, new SelectList((List<SelectListItem>)Model.Ostans,"Value","Text"),"please Select")
Check the below example.
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 AddResellerViewModel
{
public string OstanName { get; set; }
public List<SelectListItem> Ostans { get; set; }
}
Controller
public class HomeController : Controller
{
// GET: /Home/
public ActionResult Index()
{
var model = new AddResellerViewModel();
model.Ostans = PopulateOstan();
return View(model);
}
private static List<SelectListItem> PopulateOstan()
{
SqlConnection constr = new SqlConnection(System.Web.Configuration.WebConfigurationManager.ConnectionStrings["Constring"].ConnectionString);
List<SelectListItem> items = new List<SelectListItem>();
using (SqlCommand cmd = new SqlCommand("SELECT EmployeeID id,FirstName +' ' + LastName Name FROM Employees", constr))
{
cmd.CommandType = System.Data.CommandType.Text;
cmd.Connection = constr;
constr.Open();
using (SqlDataReader sdr = cmd.ExecuteReader())
{
while (sdr.Read())
{
items.Add(new SelectListItem
{
Text = sdr["Name"].ToString(),
Value = sdr["id"].ToString()
});
}
constr.Close();
}
return items;
}
}
}
View
<%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage<_Bind_DropDownListFor_SelectListItem_MVC.Models.AddResellerViewModel>" %>
<!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.OstanName, new SelectList((List<SelectListItem>)Model.Ostans, "Value", "Text"), "Please Select")%>
</body>
</html>
Screenshot