开发者

.net template 'if null' check in vb - syntax?

I am new to .net completely and I just need to know the syntax, or possible functions to put in my if statement to check if <% If Model.contacts Is Null Then%> which isn't correct.

Where Model.contacts comes from my ClientViewModel, and Model.contacts is of type System.Linq.IQueryable(Of Contact)

Here is the code for addresses...

<%  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
            &l开发者_如何学Got;/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%>

And it renders just the headers w/ more table rows from the For Each statement alt text http://a.imageshack.us/img205/236/shotji.jpg

And here is how we get Model.addresses from the ClientViewModel

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

The template is

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


There is a difference between Nothing/Null and empty (count = 0). So maybe you need to check both conditions:

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


You could try this but make sure that the Model is initialized or you might get a NullReferenceException:

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜