Drop down troubles in .net using VB and view model
OK I am new to .net, I am just taking a shot in the dark through this whole thing. I have an Addresses table with a StateID and CountryID field. They refer to the States and Countries table. I am using LINQ to SQL and Visual Studio 2010.
In my AddressesController I have:
Function Create() As ActionResult
Dim these_states = GetStates()
Dim these_countries = GetCountries()
Dim viewModel = New AddressViewModel(these_states, these_countries)
Return View(viewModel)
End Function
'
' POST: /Addresses/Create
<HttpPost()> _
Function Create(ByVal this_address As Address) As ActionResult
dataContext.Addresses.InsertOnSubmit(this_address)
dataContext.SubmitChanges()
Return RedirectToAction("Index")
End Function
I created an AddressViewModel and it looks like this:
Public Class AddressViewModel
Public Address As Address
Private _these_states As System.Linq.IQueryable(Of State)
Private _these_countries As System.Linq.IQueryable(Of Country)
Sub New(ByVal these_states As System.Linq.IQueryable(Of State), ByVal these_countries As System.Linq.IQueryable(Of Country))
_these_states = these_states
_these_countries = these_countries
End Sub
Dim StateList As New SelectList(_these_states, "StateID", "Label", 1)
Public Property States As SelectList
Get
Return StateList
End Get
Set(ByVal value As SelectList)
End Set
End Property
Dim CountryList As New SelectList(_these_countries, "CountryID", "Label", 1)
Public Property Countries As SelectList
Get
Return CountryList
End Get
Set(ByVal value As SelectList)
enter code here
Then in my view code I have:
<%@ Page Title="" Language="VB" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage(Of TotallyAwesomeCRM.AddressViewModel)" %>
some code removed as unnecessary
<div class="editor-label">
State
</div>
<div class="editor-field">
<%: Html.DropDownList("StateID", Model.States, "1")%>
<%: Html.ValidationMessageFor(Function(model) model.StateID) %>
</div>
<div class="editor-label">
Country
</div>
<div class="editor-field">
<%: Html.DropDownList("CountryID", Model.Countries, "1")%>
<%: Html.ValidationMessageFor(Function(model) model.CountryID) %>
</div>
So when I try to go to Addresses/Create I get this error:
ArgumentNullExcept开发者_StackOverflowion was not handled by user code:Value cannot be null.
Parameter name: items
And it points to this line in the AddressViewModel:
Dim StateList As New SelectList(_these_states, "StateID", "Label", 1)
I know it pulls results, so I looked up the item Parameter and msdn says it is System.Collections.IEnumerable
So I changed the AddessViewModel to:
Public Address As Address
Private _these_states As System.Collections.IEnumerable
Private _these_countries As System.Collections.IEnumerable
Sub New(ByVal these_states As System.Collections.IEnumerable, ByVal these_countries As System.Collections.IEnumerable)
_these_states = these_states
_these_countries = these_countries
End Sub
Got the same error, please help
The problem is the way you are initializing StateList and CountryList. These are values are being initialized before the New statement is called. Therefore StateList and CountryList is empty because _these_States and _these_countries are empty.
Change your code to this:
Sub New(ByVal these_states As System.Linq.IQueryable(Of State), ByVal these_countries As System.Linq.IQueryable(Of Country))
_these_states = these_states
_these_countries = these_countries
End Sub
Dim StateList As SelectList
Public Property States As SelectList
Get
If StateList Is Nothing Then
StateList = New SelectList(_these_states, "StateID", "Label", 1)
End If
Return StateList
End Get
Set(ByVal value As SelectList)
End Set
End Property
Dim CountryList As SelectList
Public Property Countries As SelectList
Get
If CountryList Is Nothing Then
CountryList = New SelectList(_these_countries, "CountryID", "Label", 1)
End If
Return CountryList
End Get
Set(ByVal value As SelectList)
' enter code here
End Set
End Property
精彩评论