Q:

Count number of records in child table in mvc

I have two classes  with one to many relationship.  I want to count the number of records in the child class based on the primary key of the parent class. 
Parent table class
  public class SystemFamily
{
    public SystemFamily()
    {
        DateCreated = DateTime. Now;
        deleteFlag = false;

    }

    public int SystemFamilyID { get; set;}
    public string FamilyName { get; set; }
    public DateTime DateCreated { get; set; } 
    public string CreatedBy { get; set; }
    public bool deleteFlag { get; set; }
    public int SystemsCount {get;set;}
    public virtual ICollection<Systems> Systems { get; set; }
    public string SFamilyName
    {
        get
        {
            return FamilyName;
        } 
    }

 }

Child table class
  public class Systems
{

    public int SystemsID { get; set; }
    public string SystemName { get; set; }
    public DateTime CreatedOn { get; set;}
    public string CreatedBy { get; set; }
    public int SystemFamilyID { get; set; }

    public virtual SystemFamily SystemFamily { get; set; }
}

I am not sure where the count should be performed.  In the view or the controller?  So i will post the required code after suggestions. 
Thanks in advance! 
Vini
EDIT 1
    public ActionResult Index()

    {
        //List<Systems> systems;
        //var query = db. SystemFamily. Select(c => c.SystemFamilyID).ToList();
        //foreach (var sid in query)
        //{
        //    systems = db. Systems. Select(c => c.SystemFamilyID == sid).ToList();
        //}
        //int count = systems. Count();//Here you will  get count

        //ViewBag. Counts = count; 
        var viewmodel = new Sys_SysFam();
        foreach (var item in db. SystemFamily)
        {
            int id = item. SystemFamilyID;

            //SystemsCount 
            int count = db. Systems. Where(x => x.SystemFamilyID == id).Count();
            item. SystemsCount = count;
        }
        ViewBag. Count = db. SystemFamily. Where(x=>x.deleteFlag==false).Count();
        int count1 =db. Systems. Count();
        ViewBag. SCount = count1;
        return View(db. SystemFamily. Where(x=>x.deleteFlag==false).ToList());
    }

This is  the action method where the logic should go I suppose!  I am trying with a foreach loop.  but it doesnt work it throws exception : 'System. Data. Entity. Core. EntityCommandExecutionException'
But I also have a ViewSystem which returns the systems in a particular family.  
ViewSystem
    public ActionResult ViewSystem(int?  id)
    {
            var viewmodel = new Sys_SysFam();
            ViewBag. SystemFamilyID = id;
            if (id ! = null)
            {
                ViewBag. SystemFamilyID = id. Value;
                var sysFamily = db. SystemFamily. Include(x => x.Systems).FirstOrDefault(x => x.SystemFamilyID == id. Value);
                if (sysFamily ! = null)
                {
                    viewmodel. Systems = sysFamily. Systems;
                }
            }

            return View(viewmodel);

    }

A:

you have to  do this in  Controller
public  ActionResult ActionMethordNam()
{
   List<Systems>  systems;
   var query = db. SystemFamily. Select(c=>c. SystemFamilyID).Tolist(); 
   foreach(var sid in query)
   { 
      systems = db. Systems. Select(c=>c. SystemFamilyID == sid).Tolist(); 
   }
   int count=systems. Count();//Here you will  get count

   Viewbag. Counts  = count; 
   return View();
}

Retrive count in View By
@Viewbag. Counts in .cshtml
@Html. TextBox("syscount", (string)ViewBag. Counts)

you Can retrieve the value in post Method by pass as  parameter to the post method now you  can save the data to your table by assigning this value to your  Model Property  

