Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Empty SelectList in ASP.NET MVC

Tags:

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.

like image 302
Rich Miller Avatar asked Jul 27 '09 21:07

Rich Miller


2 Answers

You can use Enumerable.Empty()

@Html.DropDownList("dropdown-name", Enumerable.Empty<SelectListItem>())
like image 188
Arasu RRK Avatar answered Nov 15 '22 10:11

Arasu RRK


It is possible to pass an empty list to DropDownList.

In your controller, you should have something like:

var status_code_id = mymodel.status_code_id;
var substatus_code_id = mymodel.substatus_code_id;

ViewData["status_code_id"] = new SelectList(
    StatusCodes.FindAll(), "id", "code", status_code_id);

ViewData["substatus_code_id"] = new SelectList(
    SubstatusCodes.FindAllForStatus(status_code_id), "id", "code", substatus_code_id);

...where FindAllForStatus contains the same code you're using in your AJAX call to populate the substatus dropdownlist. FindAllForStatus should take in an empty status code and return an empty list.

In your view, you should have something like:

<%= Html.DropDownList("status_code_id", "--not selected--")%>
<%= Html.DropDownList("substatus_code_id", "--not selected--")%>
like image 22
James Avatar answered Nov 15 '22 11:11

James