Question VS2022 MVC Dropdown list returning no value

PeteN

Member
Joined
Feb 25, 2008
Messages
7
Programming Experience
Beginner
I am using Visual Studios and MVC
II have a view that users create new records. On that view is a dropdown list that the user can select an option. This is not a required field.
VB.NET:
Expand Collapse Copy
        Function CreateCR() As ActionResult
            Dim rc As New WebAppEntities
            Dim LicenceNo As String = System.Web.HttpContext.Current.Session("SelectedLicenceID").ToString
            Dim _bankinggrouplist = rc.DAccountGroups.Where(Function(grp) grp.Licence = LicenceNo And grp.Active)

            Dim IndexCount As Integer = 0
            Dim ListItem As New List(Of SelectListItem) From {
                New SelectListItem With {.Text = "No Group", .Value = "0"}
            }
            If _bankinggrouplist.Count > 0 Then
                For Each result In _bankinggrouplist
                    IndexCount += 1
                    ListItem.Add(New SelectListItem With {.Text = result.Name.ToString, .Value = result.ID.ToString})
                Next
            End If
            ViewBag.Groups = _bankinggrouplist.Count
            ViewData("List") = New SelectList(ListItem, "Text", "Value", "").ToList
            rc.Dispose()
            Return View()
        End Function

on the view the dropdown list is populated, with the default option being 'No Group'

ASP.net:
Expand Collapse Copy
<div class="form-group">
    @Html.Label("Account Group")
    
    <div class="col-md-10">
        @Html.DropDownList("Account Group", New SelectList(CType(ViewData("List"), IEnumerable), "Text", "Value"))
    </div>
</div>

when clicking to create the record regardless of leaving the group default value or selecting from the list, I get an error
System.ArgumentNullException: 'Value cannot be null.
Parameter name: items'
 
Last edited by a moderator:
Back
Top