views:

37

answers:

1

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:

ArgumentNullException 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

+1  A: 

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
Tim Murphy
I can't tell you how much I appreciate your response, I have been jacking w/ this CRM on and off for a couple of weeks, it was to prove to this web business owner that I could do .NET since I have never touched it. No one seems to ever respond to my posts and I spend hours googling and trying to convert #C examples into VB. In the meantime I got laid off, so I am taking a crash course in .NET right now so I can get this done as soon as possible and hopefully get the job! That worked! BTW I may have many more posts coming lol, I have to save them now...wish me luck!
KacieHouser
Removed caveat, PylonsN00b confirmed correct. @PylonsN00b I'll add you to my favorites and see how else I can help. Clear and detailed questions as you have here makes its easy and pleasurable to answer.You may like to edit your question to get the formatting of the second code example correct.
Tim Murphy