2

Basically I want to filter customers of current business from many businesses and I want to done it on one place (constructor). So that I can easily query on db.Customers in rest of the controller. But when I use (Employee)Session["CurrentUser"]; it show error

System.Web.Mvc.Controller.Session.get returned null.

public class SOSRController : Controller
    {
        private BusinessContext db = new BusinessContext();
        public SOSRController()
        {
            Employee employee= (Employee)Session["CurrentUser"];
            db.Customers = (DbSet<Customer>) db.Customers.AsQueryable().Where(x => x.Business.Id == employee.bizId);
        }
        public ActionResult Index()
        {
            List<Customer> LstCust= db.Customers.Where(x => x.Name.Contains("asd")).ToList();
        }
}

Or what could be any other solution of filtering db.Customers at one place only?

1 Answer 1

1

It worked. I override Initialize method and now I can get session value. Constructor is concept of classes and Initialize is concept of MVC.

protected override void Initialize(RequestContext requestContext)
        {
            base.Initialize(requestContext);

             Employee employee= (Employee)Session["CurrentUser"];
            db.Customers = (DbSet<Customer>) db.Customers.AsQueryable().Where(x => x.Business.Id == employee.bizId);

        }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.