I'm using a strongly typed view with a custom viewmodel. This viewmodel contains a collection of books.
public IList<Books> Books { get; private set; }
I'm also passing a select-list to the view. However the DropDownListFor
helper class doesn't automatically preselect the default value of the book.
@Html.DropDownListFor(m => Model.Books[i].AuthorID, Model.Author as SelectList)
Everything else works, e.g. it does pass the selected value back to the controller.
I figured out a workaround by creating the select list in the viewmodel:
@Html.DropDownListFor(m => Model.Books[i].AuthorID, new SelectList(Model.AuthorList, "Value", "Text", @Model.Books[i].AuthorID.ToString()))
However I don't want to implement this workaround if there is a simpler solution. is there a way that the view can handle values from a collection?