How to add to intgers in mvc razor?

Total Post:45

Points:315

Mvc4  Razor 
 1346  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. 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());
        }
       }
    }

      Modified On Apr-06-2018 05:24:14 AM

Answer

Please check, If you want to make this post sponsored

You are not a Sponsored Member. Click Here to Subscribe the Membership.