0

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:

enter image description here

But instead it renders like this:

enter image description here

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.

Ben
  • 4,281
  • 8
  • 62
  • 103
  • Note also that adding the 4th parameter or the `SelectList` constructor ('row.BillingStatusID') is pointless since its ignored by `DropDownListFor()`. Internally the method builds a new `IEnumerable` and sets the `Selected` property based on the value of the property your binding to –  Jul 24 '17 at 11:56
  • Have just realized you have `m => item.BillingStatusID` which suggest that your probably doing this in a `foreach` loop which will never work anyway - refer [this answer](http://stackoverflow.com/questions/30094047/html-table-to-ado-net-datatable/30094943#30094943) for why a `foreach` loop will not work and [this answer](http://stackoverflow.com/questions/37407811/mvc5-razor-html-dropdownlistfor-set-selected-when-value-is-in-array/37411482#37411482) to explain how to use `DropDownListFor()` for a collection –  Jul 24 '17 at 11:59

1 Answers1

0
var data=Model.Select(o=> new SelectListItem{
 Value=o.id.toString(),
 Text=o.value,
 Selected=o.id==yourSelectId?true:false
}).toList();

and then your view page should be like-

<option value="@option.value" selected="option.Selected">@option.text</option>
Sohel Rana
  • 181
  • 2
  • 14
  • Can you expand on this? Not sure how / where this ties in to what I have above? – Ben Jul 24 '17 at 11:53
  • Your question marked as duplicate you could look after the answer. But in my case, i use like – Sohel Rana Jul 24 '17 at 12:03
  • 1
    @SohelRana, That is bad code and will never give true 2-way model binding (which use `ModelState` to set the value if it exists) and will not give client side validation. –  Jul 24 '17 at 12:16
  • And since `selected="true"`and `selected="false"` (or `selected="AnythingAtAll"`) all mean that the option will be selected (its the presence of the attribute that determines if an option is selected), only the last option will ever be selected. –  Jul 24 '17 at 12:28