HOW TO ADD TO INTGERS IN MVC RAZOR?

Norman Reedus

Total Post:45

Points:315
Posted by  Norman Reedus
Mvc4  Razor 
 908  View(s)
Ratings:
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().

  1. Alex Leblois

    Post:67

    Points:471
    Re: How to add to intgers in mvc razor?

    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());

        }

       }

    }

Answer

NEWSLETTER

Enter your email address here always to be updated. We promise not to spam!