简体   繁体   English

计算MVC子表中的记录数

[英]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 编辑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. 我正在尝试使用foreach循环。 but it doesnt work it throws exception : 'System.Data.Entity.Core.EntityCommandExecutionException' 但它不起作用,它会引发异常: 'System.Data.Entity.Core.EntityCommandExecutionException'

But I also have a ViewSystem which returns the systems in a particular family. 但是我还有一个ViewSystem,它可以返回特定系列的系统。

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

    }

you have to do this in Controller 您必须在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 .cshtml中的@Viewbag.Counts

@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 您可以通过将参数传递给post方法来检索post Method中的值,现在您可以通过将该值分配给Model属性来将数据保存到表中

 var dataList =  db.SystemFamily.Where(x => x.SystemFamilyID == id.Value).SelectMany(s => s.Systems).GroupBy(key => key.SystemFamilyID, sys => new {sys},
               (i, sys) =>
               new SystemsViewModel{
                 System = sys,
                 Count = sys.Count()
               }).ToList();

pass the dataList to the View 将dataList传递给View

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM