This is my first attempt to populate a dropdown using a List<> collection. I am not quite sure I am doing this right, so any instruction would be great. I don't think I am getting the right information to the list in the view?
My model looks like:
public List<LOB> LOBTypes
{
get
{
try
{
using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["MNB_connectionstring"].ConnectionString))
{
SqlCommand cmd = new SqlCommand("SELECT CODE, NAME FROM BOA_LOB_Details ORDER BY NAME", con);
cmd.CommandType = CommandType.Text;
SqlDataReader dr = cmd.ExecuteReader();
if (dr.HasRows)
{
while (dr.Read())
{
_LOBTypes.Add(new LOB { Code = dr["CODE"].ToString(), Name = dr["NAME"].ToString() });
}
}
}
}
catch (Exception ex)
{
ExceptionUtility.LogException(ex, "GetLOBTypes()");
}
return _LOBTypes;
}
}
My view contains:
@Html.Label("Product Type: *");
@Html.DropDownListFor(m => m.LOBTypes, new SelectList(Model.LOBTypes, "NAME"));