根据ID中的列表会话中存储的数据从数据库中检索数据

时间:2018-08-02 11:53:23

标签: c# model-view-controller

如何基于ID中存储在会话中的列表中的数据从数据库中检索数据?

该类用于保存列表中我页面中的数据选择。

  public class com
{
    private Category ct = new Category();
    public Category Ct
    {
        get { return ct; }
        set { ct = value; }
    }
    public com() { }
    public com(Category ct)
    {
        this.Ct = ct;
    }
}

}

这是将数据选择添加到列表的功能。

  public ActionResult Comp(int idcat)
    {
        if (Session["com"]==null)
        {
            List<com> compa = new List<com>();
            compa.Add(new com(db.Categories.Find(idcat)));
            Session["com"] = compa;
        }
        else
            {
            List<com> compa = (List<com>) Session["com"];
            ViewBag.ind = count(idcat);
            if (ViewBag.ind == -1)
            {
            compa.Add(new com(db.Categories.Find(idcat)));
                Session["com"] = compa;
            }
            else if (ViewBag.ind == 0)
            {
                ViewBag.Message = "You Have 4 Car is Limited";
            }
        }
        return PartialView("_Compare");
    }

这是用于根据上一类检索数据的类。

 public class specsat
{
    private Spec sp = new Spec();

    public Spec Sp
    {
        get
        {
            return sp;
        }

        set
        {
            sp = value;
        }
    }


    public specsat() {}
    public specsat(Spec sp)
    {
        this.Sp = sp;
    }
}

}

并具有检索数据的功能。

public ActionResult Detail()
    {
          List<com> list = (List<com>)Session["com"]; 
        List<specsat> spe = new List<specsat>();
        foreach (var co in list)
        {
            if (co.Ct != null)
            {
                spe.Add(new specsat(db.Specs.Find( co.Ct.id)));
            }
        }
        return View(spe);
    }

最后显示了一些数据,但显示了另外的数据。

“对象引用未设置为对象的实例”

0 个答案:

没有答案