Click here to Skip to main content
15,885,216 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
if (Model.Count() < 2)
{

}


Dear sir

I Want to Know How to Check if Model. count = null then do not throw exception and it goes in inside of Codtion

What I have tried:

sir when it get null value then condition should true when model.value=1 then condition true but when model. value =2 then condition false


so please help me
Posted
Updated 6-Jul-17 22:59pm
Comments
F-ES Sitecore 7-Jul-17 5:03am    
What type is Model? What type does "Count()" return?

Try with this:
if (Model!=null) && (Model.Count<2)) <br />
{<br />
}


Explanation: only when the model is not null (fisrt condition) chek if the model.count is less than 2
 
Share this answer
 
Comments
Member 12183079 7-Jul-17 4:24am    
i am getting error in &&
Member 12183079 7-Jul-17 4:25am    
invalid Expression Item &&
Member 12183079 7-Jul-17 4:54am    
Yes it is working but when it goes in else condition then geting error in
else{
foreach (var p in Model)
{











@p.Farmer_Name



}


Object reference not set to an instance of an object.
I think that the problem is that Model is null, and you do not expect that.
 
Share this answer
 
Comments
Member 12183079 7-Jul-17 5:06am    
how to use when foreach (var p in Model)
{
is null then it does not get error
Member 7870345 7-Jul-17 5:39am    
If Model is null, its imposible to enumerate Model (basically what foreach does).
Conceptually it has no sense
How to iterate, enumerate somethis that not exists?

In my opinion there is a previous error that makes the Model null (perhaps a bad parameter in the call)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900