Home > DeveloperSection > Forums > How to add to intgers in mvc razor?
Norman Reedus
Norman Reedus

Total Post:45

Points:315
Posted on    November-10-2014 2:27 AM

 ASP.NET MVC Mvc4  Razor 
Ratings:


 1 Reply(s)
 604  View(s)
Rate this:

No's Parents :

@{

   foreach (var item in Model)

   {

  ((item.Mothers.Count) + (item.Fathers.Count)).ToString();

   }

}

O/P: No's Parents:

No's Parents :

@{

foreach (var item in Model)

   {

 item.Mothers.Count +item.Fathers.Count

   }

}

O/p: No's Parents :0+0

I trying to show o/p like, if parents(mother/ father) information not found then display with zero, if mother/ father information exists then display additioinal of Mother.count()+Father.Count().



Alex Leblois
Alex Leblois

Total Post:67

Points:471
Posted on    November-10-2014 5:20 AM

You can check to see if the sum of both entities is 0, then take action on the result.

This will write "no parents" if both the mothers and fathers items are 0. Otherwise, it will write the number of parents.

@{

   foreach (var item in Model)

   {

    if(item.Mothers.Count == 0 && item.Fathers.Count == 0)

    {

        response.write("no parents");

    }else{

        response.write(((item.Mothers.Count) + (item.Fathers.Count)).ToString());

    }

   }

}


Don't want to miss updates? Please click the below button!

Follow MindStick