I have the following DropDownListFor. The ShopId entry is nullable and I would like to keep it that way. This dropdownlist displays the shops as follows:
@Html.DropDownListFor(m => weekDay.Shop.ShopId,
new SelectList(Model.ShopItems,
"ShopId",
"Name",
weekDay.Shop.ShopId),
""
)
Now I want to have the application not give a System.NullReferenceException: Object reference not set to an instance of an object-error when the value is NULL. I've tried using if-statements and try-catches. Besides being ugly they do not work.
Does anyone have an idea how I can display a default empty string when the value in the database is NULL?
/edit The same question in another way. The controller looks as follows:
var viewModel = new ShopWeekDayViewModel
{
ShopItems = db.Shops.ToList(),
WeekDays = db.WeekDays.ToList()
};
How can I filter empty the null values in ShopItems and change them into empty strings?
/edit 2
The values in the database look as follows:
WeekDayId ShopId Day 1 NULL Zondag 2 1 Maandag 3 1 Dinsdag 4 1 Woensdag 5 1 Donderdag 6 2 Vrijdag 7 NULL Zaterdag
When I change the NULL values to 1 or 2 the code works...