public ActionResult Index()
        {
            String user = User.Identity.Name;
            //user = "******";
            ViewData["userName"] = user;

            List<String> l = programmes.Keys.ToList<string>();
            l.Insert(0,"Please Select");
            SelectList faculties = new SelectList(l);
       
            ViewData["faculties"] = faculties;

            RankingContext db = new RankingContext();
            QueryManager mng = new QueryManager(db);

            DateTime end = Convert.ToDateTime(mng.getCampaignDates().PreferrencesLastDate);
            ViewData["isAddingPreferencesEnd"] = false;
            if (DateTime.Today > end)
            {
                ViewData["isAddingPreferencesEnd"] = true;
            }



            QueryManager queryManager = new QueryManager(db);

            List<Preference> studentPreferences = queryManager.getStudentPreferences(user);

            foreach (var preff in studentPreferences)
            {
                Faculty f = db.Faculties.Find(preff.ProgrammeName);
                String fac = (f != null ) ? f.FacultyName : "";
                StudentPreferences pr = new StudentPreferences
                {
                    Faculty = fac,
                    ProgrammeName = preff.ProgrammeName,
                    PrefNumber = preff.PrefNumber
                };
                model.Add(pr);
            }
            ViewData["result"] = model;

            return View(model);
        }
        public ActionResult Index(String faculty, String programmeName)
        {
            String user = User.Identity.Name;
            ViewData["userName"] = user;


            RankingContext db = new RankingContext();
            QueryManager mng = new QueryManager(db);

            DateTime finale = Convert.ToDateTime(mng.getCampaignDates().PreferrencesLastDate); 
            

            ViewData["isAddingPreferencesEnd"] = true;

            if (DateTime.Today > finale)
            {
                ViewData["isAddingPreferencesEnd"] = true;
            }


            List<String> l = programmes.Keys.ToList<string>();
            l.Insert(0, "Please Select");
            SelectList faculties = new SelectList(l);

            ViewData["faculties"] = faculties;

            

            String egn = user;

            //int lastPreferenceNumber = db.Preferences.Where(t => t.EGN == egn )
            //                                         .OrderByDescending(t => t.PrefNumber)
            //                                         .FirstOrDefault().PrefNumber;

            QueryManager queryManager = new QueryManager(db);

            List<Preference> studentPreferences = queryManager.getStudentPreferences(egn);

            bool isPreferrenceRepeated = false;
            foreach (var preff in studentPreferences)
            {
                String fac = db.Faculties.Find(preff.ProgrammeName).FacultyName;
                StudentPreferences pr = new StudentPreferences { Faculty = fac, ProgrammeName = preff.ProgrammeName,
                                                                 PrefNumber = preff.PrefNumber };
                if (faculty == fac && preff.ProgrammeName == programmeName)
                {
                    isPreferrenceRepeated = true;
                }

                model.Add(pr);
            }

            
           
            if (!isPreferrenceRepeated)
            {
                int nextPrefenceNumber = (studentPreferences.Count() != 0) ? studentPreferences.Max(t => t.PrefNumber) + 1 : 1;
                StudentPreferences pref = new StudentPreferences
                {
                    Faculty = faculty,
                    ProgrammeName = programmeName,
                    PrefNumber = nextPrefenceNumber
                };
                Preference p = new Preference
                {
                    EGN = egn,
                    PrefNumber = nextPrefenceNumber,
                    ProgrammeName = programmeName,
                    TotalGrade = 0
                };

                db.Preferences.Add(p);
                db.SaveChanges();
                model.Add(pref);
            }
            
            ViewData["result"] = model;



            return PartialView("_StudentPreferencesTable", model);
            //return View("Index",model);
        }