I believe I have bound my data correctly, but I can't seem to get my text property for each SelectListItem to show properly.
My model:
public class Licenses
{
public SelectList LicenseNames { get; set; }
public string SelectedLicenseName { get; set; }
}
Controller:
[HttpGet]
public ActionResult License()
{
try
{
DataTable LicsTable = BW.SQLServer.Table("GetLicenses", ConfigurationManager.ConnectionStrings["ProfressionalActivitiesConnection"].ToString());
ProfessionalActivities.Models.Licenses model = new ProfessionalActivities.Models.Licenses();
model.LicenseNames = new SelectList(LicsTable.AsEnumerable().Select(row =>
new SelectListItem
{
Value = row["Description"].ToString(),
Text = "test"
}));
return PartialView("_AddLicense", model);
}
catch (Exception ex)
{
var t = ex;
return PartialView("_AddLicense");
}
}
View:
@Html.DropDownList("LicenseNames", new SelectList(Model.LicenseNames, "Value", "Text", Model.LicenseNames.SelectedValue), new { htmlAttributes = new { @class = "form-control focusMe" } })
DropDownList()
isIEnumerable<SelectListItem>
. You create that usingnew SelectListItem { Value = row["Description"].ToString(), Text = "test"})
which is all you need. Creating a 2nd identical one in the controller usingnew SelectList()
(where you forgot to set theText
andValue
properties) and then creating a 3rd identical one in the view is just pointless extra overhead.