使用VB和视图模型减少.net中的麻烦

时间:2010-07-12 21:49:51

标签: asp.net-mvc vb.net drop-down-menu viewmodel

好的我是.net的新手,我只是在黑暗中通过整个事情拍摄。我有一个带有StateID和CountryID字段的Addresses表。它们指的是“国家和国家”表。我正在使用LINQ to SQL和Visual Studio 2010.

在我的AddressesController中,我有:

    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

我创建了一个AddressViewModel,它看起来像这样:

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

然后在我的视图代码中我有:

    <%@ Page Title="" Language="VB" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage(Of TotallyAwesomeCRM.AddressViewModel)" %>

将某些代码删除为不必要的

            <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>

因此,当我尝试转到地址/创建时,我收到此错误:

ArgumentNullException was not handled by user code:Value cannot be null.

参数名称:项目

它指向AddressViewModel中的这一行:

Dim StateList As New SelectList(_these_states, "StateID", "Label", 1)

我知道它会拉结果,所以我查找了项目参数,msdn说它是System.Collections.IEnumerable

所以我将AddessViewModel更改为:

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

得到同样的错误,请帮忙

1 个答案:

答案 0 :(得分:1)

问题在于初始化StateList和CountryList的方式。这些值是在调用New语句之前初始化的。因此,StateList和CountryList为空,因为_these_States和_these_countries为空。

将您的代码更改为:

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