I have a number of ListBoxFor elements on a form in edit mode. If there was data recorded in the field then the previously selected items are displaying correctly when the form opens. If the field is empty though an error is thrown as the items parameter cannot be null. Is there a way to check in the view and if there is data to use the ListBoxFor with the four parameters but if there isn't to only use three parameters, leaving out the selected items?
This is how I'm declaring the ListBoxFor:
@Html.ListBoxFor(model => model.IfQualityPoor, new MultiSelectList(ViewBag.IfPoor, "Value", "Text", ViewBag.IfQualityPoorSelected), new { @class = "chosen", multiple = "multiple" })
I'm using the ViewBag to pass the ICollection which holds the selected items as the controller then joins or splits the strings for binding to the model field. The MultiSelectLists always prove problematic for me.
Your question isn't entirely clear, but you're making it way harder on yourself than it needs to be using
ListBoxFor. All you need for eitherDropDownListFororListBoxForis anIEnumerable<SelectListItem>. Razor will take care of selecting any appropriate values based on theModelState.So, assuming
ViewBag.IfPoorisIEnumerable<SelectListItem>, all you need in your view is:The correct options will be marked as
selectedbased on the value ofIfQualityPooron your model, as they should be. Also, it's unnecessary to passmultiple = "multiple"in in yourhtmlAttributesparam, as you get that just by usingListBoxForrather thanDropDownListFor.It's even better if you use a view model and then add your options as a property. Then, you don't have to worry about casting in the view, which is always a good way to introduce runtime exceptions. For example:
Then, you set this in your action, before returning the view (instead of setting
ViewBag). Finally, in your view:Much simpler, and you'll never have any issues doing it that way.
UPDATE (based on comment)
The best way to handle flattening a list into a string for database storage is to use a special property for that, and then custom getter and setter to map to/from. For example:
Then, you post to/interact with
IfQualityPoorList, and the correct string will be set in the database automatically when you save.