2010-07-12 35 views
1

好的我是.net新手,我只是在黑暗中通过整个事情拍摄。我有一个具有StateID和CountryID字段的地址表。他们提到了国家和国家的表格。我使用LINQ to SQL和Visual Studio 2010中在.net中使用VB下载麻烦并查看模型

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

的问题是要初始化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 
+0

我不能告诉你我是多么感激您的回复,我已经顶瓦特/这CRM和关闭了几个星期,就被证明对这个网络业主来说,我可以做.NET,因为我从来没有碰过它。似乎没有人回应我的帖子,我花了数小时搜索并尝试将#C示例转换为VB。 与此同时,我被解雇了,所以我现在正在.NET中进行速成课程,以便尽快完成此项任务,并希望能够得到这份工作! 工作正常!顺便说一句,我可能会有更多的帖子来大声笑,我现在要拯救他们...祝我好运! – KacieHouser 2010-07-13 03:43:28

+0

删除警告,PylonsN00b确认无误。 @ PylonsN00b我会把你添加到我的最爱,看看我可以怎样帮助。清晰细致的问题让您轻松愉快地回答。 您可能希望编辑您的问题以获取正确的第二个代码示例的格式。 – 2010-07-13 06:10:37