When you use @Html.DropDownListFor(...) with an object property, it will use the current state of the object to set the selected item. Therefore, in your example, if you do not specify a value in the select list, it will use the value of m.EndsOnHour to set the selected value. In fact, the select list does not even have to be a SelectList for each user, it really needs to be a set of SelectListItem , as you can see in the documentation .
However, the problem with serialization still exists. You have a couple of options, the easiest of which is to simply throw away the values ββon the view side, if possible. Many drop-down lists are created for static selection to display possible enumeration options, such as, for example, listing states or countries. Other common examples are date ranges, etc. In these cases, you can write helpers that will generate these lists and call them in your view, and then simply remove them from your view model.
If the list is dynamic, in the long run it does not work very well and is likely to cause additional communication, which will negatively affect the maintenance of your system. In this case, you will need to create your own child class SelectList , which is serialized, and then use it as a parameter of the type type of the serializable implementation of IEnumerable<T> . Arrays in C # are serializable, so you need this part.
source share