– Empty SelectList in ASP.NET MVC

I'm sure this is a common operation, but I'm having a hard time getting my head around a solution. It's the problem of two <select> on a web page where one depends on the other for its list of possible values.

I have a dropdown list containing substatus codes on a web page whose list of possible values depends upon the selection in another dropdown list on the page that contains status codes. In my controller, I retrieve the list of possible status codes from the database and put the values in a SelectList that is used by the DropDownList HTMLHelper to build my select list in the view. I retrieve a model record that contains a status code and use that status code value to look up the possible values of substatus in the database, and I put that list in another SelectList for the second dropdown. The page makes an ajax call back to the server to repopulate the substatus dropdown with a new list of possible values when the user changes the selection in the status dropdown.

An empty status code really isn't a valid value, but this is a quality control app that is used to correct such problems. My problem is that the current status code on the model record can sometimes be empty and therefore my list of substatus values for the second dropdown will be empty. Unfortunately there seems to be no way to create an empty SelectList to pass to the DropDownList HTMLHelper, but my dropdown list must be created when the page loads to support my ajax solution.

Has anyone worked out an efficient and super slick way to handle such scenarios? Or do I need to resort to something ugly like checking in my view if the substatus SelectList is null, and if it is using different HTML to generate the dropdown? It seems in my sick mind that being able to generate an empty SelectList to hand off to the HTMLHelper would be a reasonable solution. I'd welcome any ideas.

Best Solution

You can use Enumerable.Empty()

@Html.DropDownList("dropdown-name", Enumerable.Empty<SelectListItem>())