Asp.net mvc 用VB和视图模型实现.net中的下拉故障

Asp.net mvc 用VB和视图模型实现.net中的下拉故障,asp.net-mvc,vb.net,drop-down-menu,viewmodel,Asp.net Mvc,Vb.net,Drop Down Menu,Viewmodel,好吧,我是.net的新手,我只是在黑暗中拍摄这整件事。我有一个地址表,其中有StateID和CountryID字段。它们指的是州和国家表。我正在使用LINQtoSQL和VisualStudio2010 在我的地址控制器中,我有: Function Create() As ActionResult Dim these_states = GetStates() Dim these_countries = GetCountries() Dim v

好吧,我是.net的新手,我只是在黑暗中拍摄这整件事。我有一个地址表,其中有StateID和CountryID字段。它们指的是州和国家表。我正在使用LINQtoSQL和VisualStudio2010

在我的地址控制器中,我有:

    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
在我的视图代码中,我有:

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

它指向AddressViewModel中的这一行:

Dim StateList As New SelectList(_these_states, "StateID", "Label", 1)
我知道它会提取结果,所以我查找了item参数,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

遇到相同的错误,请帮助

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

将代码更改为:

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

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

将代码更改为:

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

我无法告诉你我有多感激你的回复,我已经断断续续地劫持了这个CRM好几个星期了,这是为了向这个网络企业主证明我可以使用.NET,因为我从未接触过它。似乎从来没有人回复过我的帖子,我花了几个小时在谷歌上搜索并试图将#C示例转换成VB。在此期间,我被解雇了,所以我现在正在参加一个.NET速成班,这样我就可以尽快完成这项工作,希望能得到这份工作!成功了!顺便说一句,我可能会有更多的职位来笑,我必须保存他们现在…祝我好运!已删除警告,塔架N00B已确认正确@我会把你添加到我的收藏夹中,看看还有什么我可以帮忙的。你在这里提出的清晰而详细的问题使得回答起来简单而愉快。您可能想编辑您的问题,以使第二个代码示例的格式正确。我无法告诉您我有多感激您的回答,我已经断断续续地劫持了这个CRM好几个星期了,这是为了向这个web业务所有者证明我可以使用.NET,因为我从未接触过它。似乎从来没有人回复过我的帖子,我花了几个小时在谷歌上搜索并试图将#C示例转换成VB。在此期间,我被解雇了,所以我现在正在参加一个.NET速成班,这样我就可以尽快完成这项工作,希望能得到这份工作!成功了!顺便说一句,我可能会有更多的职位来笑,我必须保存他们现在…祝我好运!已删除警告,塔架N00B已确认正确@我会把你添加到我的收藏夹中,看看还有什么我可以帮忙的。你在这里提出的清晰而详细的问题使得回答起来简单而愉快。您可能需要编辑问题,以使第二个代码示例的格式正确。
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
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