In my view I have the following DropDownListFor:
@Html.DropDownListFor(m => item.BillingStatusID, (SelectList)ViewBag.BillingStatusID, "Select...")
I am using the following controller, which is used to both edit and create:
[HttpGet]
public ActionResult Create()
{
var week = new Week();
ApplicationDbContext db = new ApplicationDbContext();
var timeEntries = from te in db.TimeEntries
select te;
ViewBag.BillingStatusID = new SelectList(db.BillingStatus, "BillingStatusID", "Name", row.BillingStatusID);
foreach (TimeEntry t in timeEntries)
{
week.TimeEntries.Add(t);
}
week.TimeEntries.Add(new TimeEntry { TimeEntryID = -1 });
return View(week);
}
However, when the page renders it shows the selected value as "Select..." instead of the billing status for that item.
The desired result is for the page to render like this:
But instead it renders like this:
If I change the ViewBag to:
ViewBag.BillingStatusID = new SelectList(db.BillingStatus, "BillingStatusID", "Name", 2);
for example, it works and the DropDownListfor is populated with the entry with BillingStatusID = 2, but obviously this isn't the desired behaviour. How can I ensure that the index is returned properly from the ViewBag?
My suspicion is that I should be somehow passing a variable back to the controller, but I'm new to MVC, so not sure how this should work.