I have 2 dropdownlist whose values should be populated from database. I have checked the stored proc and its giving correct results. I have even debugged upto controller and it looks like I am getting right values.
I need help in figuring out the @Html.DropDownlistFor syntax. What i am using is incorrect. Any help?
Model:
public class DropDownConfiguration
{
public string Project { get; set; }
public string LineID { get; set; }
}
Controller:
[ActionName("DetailsForm")]
[HttpGet]
public ActionResult DetailsForm()
{
try
{
IEnumerable<DropDownConfiguration> dropConfig = new List<DropDownConfiguration>();
dropConfig = floorService.DropDownList();
DetailsViewModel model = new DetailsViewModel()
{
dropConfig = dropConfig,
};
return View("DetailsForm",model);
}
catch (Exception ex)
{
return View("_error");
}
}
Repository:
public IEnumerable<DropDownConfiguration> DropDownList()
{
var model = new List<DropDownConfiguration>();
using (var dbCmd = defaultDB.GetStoredProcCommand("dbo.GetLineIDProject"))
{
var varCmd = defaultDB.ExecuteReader(dbCmd);
while (varCmd.Read())
{
model.Add(new DropDownConfiguration()
{
LineID = varCmd.IsDBNull(0) ? "" : varCmd.GetString(0),
Project = varCmd.IsDBNull(1) ? "" : varCmd.GetString(1),
Quarter = varCmd.IsDBNull(2) ? 0 : varCmd.GetInt32(2),
Year = varCmd.IsDBNull(3) ? 0 : varCmd.GetInt32(3)
});
}
}
return model;
}
Html (Here are the 2 dropdown list LineID, Project)-----NEED HELP HERE------
@Html.DropDownListFor(m => m.dropConfig.FirstOrDefault().LineID, "--Select LineID--", new { @class = "form-control" })
@Html.DropDownListFor(m => m.dropConfig.FirstOrDefault().Project, "--Select Project--", new { @class = "form-control" })