2

Currently my code looks like this:

@Html.DropDownListFor( m => m.regionIdentification, new SelectList(Model.UI.region, 
    "Value", "Text", Model.region), new {@class = "form-control input-sm"})

I'm looking for a simple was to ignore nulls in the list when I populate the DropDown.

For example:

.notNull()

I'm trying to avoid having to use linq or update stored procedures. Any suggestion would be helpful, thanks!

jShoop
  • 411
  • 1
  • 6
  • 15

1 Answers1

2

I think you have several options:

  1. Extend SelectList class to add the ability to filter out the values that are not valid in the constructor, and then use your custom class instead of SelectList.
  2. Use a Razor inline function to filter the values that are null, since you don't prefer LINQ. See this link for a sample inline function syntax: http://weblogs.asp.net/hajan/functions-inside-page-using-razor-view-engine-asp-net-mvc
  3. Do some more validation in the Controller or Model class when you are populating the list of regions to prevent adding invalid values in the collection.
  4. Create your own collection that does not allow Null values, or extend one of the existing collections, so you make sure your collection contains only valid values. Read this for ideas: Are there any collections in .NET that prevent null entries?
  5. Use a LINQ extension method in Razor like .Where(r=> r != null)

I would still recommend using LINQ (no. 5 in my list), since it's the simplest and safest approach.

Community
  • 1
  • 1
Faris Zacina
  • 14,056
  • 7
  • 62
  • 75