If Model.ProjectItems is of type IEnumerable then you can do:
if (Model.ProjectItems != null && Model.ProjectItems.Any())
{
// do something
}
So basically you check it's not NULL and then you check if the sequence contains any items. The Any() method is an extension method within System.Linq namespace.
HasProperty and HasValue checking strongly typed models
Hi,
I am using the ModelsBuilder and using strongly typed syntax in my Views.
I want to know the Strongly typed equivalent/best practice to using HasValue/HasProperty is.
For a string property named 'projectTitle', I have used the following
But I am struggling trying to do similar for a Multinode Tree picker property named 'projectItems' as that returns a IEnumerable .
I have ended up using this, which works, but this defeats object of using strongly typed
What is best practice in this situation?
Thanks Simon
If
Model.ProjectItems
is of typeIEnumerable
then you can do:So basically you check it's not NULL and then you check if the sequence contains any items. The
Any()
method is an extension method withinSystem.Linq
namespace.Thanks Dan - that worked perfectly
is working on a reply...