I am trying to define a default value for my DropDown list as per another post on Stack Overflow as:
@Html.DropDownList(Function(model) model.Type, DirectCast(ViewBag.Type, IEnumerable(Of SelectListItem)), "None Selected", "No Payment")
But am getting an error of:
“Lambda expression cannot be converted to ‘String’ because ‘String’ is not a delegate type.”
How to I achieve this so if nothing is selected in returns “No Payment”.
Well, first, I think you must choose
DropDownListFor, notDropDownList, if your first argument is anExpression(Of Func(Of TModel, TProperty))In both case, there’s no overload of
DropDownList/Forwhich takes 2 strings at the end, one for default text, and the other for default value.If you do
You’ll get a null value for Model.Type when “None Selected” is the choosen option.
If you want to completely manage the default value and text, you’ll have to add it directly to your
SelectListItem(so inViewBag.Type).And use
But not sure it’s usefull. With other solution, you can check if “None Selected” has been selected by something like that (I do c#, forgive the syntax errors)