.net шаблон "если ноль" проверить в VB - синтаксис? - PullRequest
0 голосов
/ 14 июля 2010

Я новичок в .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 альтернативный текст http://a.imageshack.us/img205/236/shotji.jpg

А вот как мы получаем Model.addresses из 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

Шаблон

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

Ответы [ 2 ]

1 голос
/ 14 июля 2010

Существует разница между 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
%>
0 голосов
/ 14 июля 2010

Вы можете попробовать это, но убедитесь, что Модель инициализирована, или вы можете получить NullReferenceException:

<%  If Model.contacts Is Nothing Then %>
    <div>no contacts</div>
<%  End If %>
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...