.net模板'if null'签入vb - 语法?

时间:2010-07-14 04:24:56

标签: asp.net-mvc templates syntax if-statement

我是.net的新手。我只需要知道语法或可能的函数来放入我的if语句来检查<% If Model.contacts Is Null Then%>是否不正确。

Model.contacts来自我的ClientViewModel,而Model.contacts的类型为System.Linq.IQueryable(Of Contact)

以下是地址代码......

<%  If Model.addresses Is Nothing Then %>
        <table class="edit">
          <tr>
            <td>
            There are no Addresses associated with this Client, click the 'Create' Button to add contacts.
            </td>
          </tr>
        </table>
    <% Else%>
    <table class="child">
        <tr>
            <th>
                Actions
            </th>
            <th>
                Street
            </th>
            <th>
                City
            </th>
            <th>
                State
            </th>
            <th>
                Country
            </th>
            <th>
                Zip
            </th>
        </tr>

    <% For Each item In Model.addresses%>

        ... shows more table rows...

    <% Next%>

    </table>
    <% End If%>

它只呈现来自For Each语句的更多表行的标题 alt text http://a.imageshack.us/img205/236/shotji.jpg

以下是我们从ClientViewModel

获取Model.addresses的方法
Public Class ClientViewModel
Private _this_client As Client
Private _these_contacts As System.Linq.IQueryable(Of Contact)
Private _these_addresses As System.Linq.IQueryable(Of Address)
Private _these_statuses

Sub New(ByVal this_client As Client, ByVal these_contacts As System.Linq.IQueryable(Of Contact), ByVal these_addresses As System.Linq.IQueryable(Of Address), ByVal these_statuses As System.Collections.IEnumerable)
    _this_client = this_client
    _these_contacts = these_contacts
    _these_addresses = these_addresses
    _these_statuses = these_statuses
End Sub

Public ReadOnly Property contacts As System.Linq.IQueryable(Of Contact)
    Get
        Return _these_contacts
    End Get
End Property

Public ReadOnly Property addresses As System.Linq.IQueryable(Of Address)
    Get
        Return _these_addresses
    End Get
End Property

模板是

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

2 个答案:

答案 0 :(得分:1)

Nothing / Null和empty(count = 0)之间存在差异。所以也许你需要检查这两个条件:

<% If Model.contacts Is Nothing 
   ' do something 
   Else
       If Model.contacts.Count = 0 Then
           ' do something
       Else
           ' do something
       End If
   End If
%>

答案 1 :(得分:0)

您可以尝试这样做,但要确保模型已初始化,或者您可能会获得NullReferenceException

<%  If Model.contacts Is Nothing Then %>
    <div>no contacts</div>
<%  End If %>