public ActionResult Edit(CompletedApp completedapp)
 {
     if (ModelState.IsValid)
     {
         db.Entry(completedapp).State = EntityState.Modified;
         db.SaveChanges();
         return RedirectToAction("Index");
     }
     return View(completedapp);
 }
        public ActionResult Create(CompletedApp completedapp)
        {
            if (ModelState.IsValid)
            {
                db.CompletedApps_DbSet.Add(completedapp);
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            return View(completedapp);
        }
 public ActionResult ViewApp(CompletedApp app)
 {
     if (ModelState.IsValid)
     {
         db.Entry(app).State = EntityState.Modified;
         db.SaveChanges();
         return RedirectToAction("ViewApps");
     }
     return View(app);
 }
        public ActionResult Results(FormCollection collection)
        {
            //True = passed questionaire
            //False = failed questionaire
               int results = 0;
               /*Grading Code
            *   Check all answers to determine there are no wrong answers
            *       Find the associated question, check that it isn't short answer.
            *       if it is short answer then check that the value isn't null or empty.
            *       if it isn't short answer check that the value matches the other value.
            *   if correct, save it
            *   otherwise, go to fail screen.
            */
            String[] collectionKeys = collection.AllKeys;
            var udContext = context.UserData_DbSet;
            var appcontext = context.CompletedApps_DbSet;
            var qcontext = context.Question_DbSet;

            bool pass = true;
            String collectionValue = "";
            for(int i = 0; i < collectionKeys.Length; i++)
            {
                String[] id = collectionKeys[i].Split('_');
                var q = qcontext.Find(Convert.ToInt32(id[2]));
                collectionValue = collection.GetValue(collectionKeys[i]).AttemptedValue;
                if( q != null )
                {
                    if( q.QuestionType == 0 && null == collectionValue)
                    {
                        pass = false;
                        break;
                    }
                    else if( q.QuestionType != 0 && q.CorrectAns != Convert.ToInt32(collectionValue) )
                    {
                        pass = false;
                        break;
                    }
                }
            }
            /* some psuedo code
             * try to do the following
             *      Insert the answers into the completed applications DbSet
             *      Parse through the list of answers and encapsulate it as a string as follows
             *                  A_<type>_<id>_<Input Answer>;A_<type>_<qsetid>_<id>_<Input Answer>;
             *      Save all the other session variables needed (userInformation)
             *      save the context
             *      Display win
             * if it fails display a page that says the application process failed and needs to be restarted.
             *      Don't do anything with the information
             *      Reinitialize the datamembers
             */
              /*Save data code.*/
            if (pass)
            {
                try
                {

                    List<String> userInformation = (List<String>)HttpContext.Session["userInformation"];

                    String appString = "";

                    List<int> selectedJobIds = (List<int>)HttpContext.Session["selectedJobIds"];
                    for (int i = 0; i < collectionKeys.Length; i++)
                    {
                        appString += collectionKeys[i] + "_" + collection.GetValue(collectionKeys[i]).AttemptedValue + ";";
                    }
                    String positions = "";
                    foreach(int i in selectedJobIds)
                    {
                        positions += context.Job_DbSet.Find(i).Position + ";";
                    }
                    UserData userdat;
                    if (userInformation.Count == 8)
                    {
                        userdat = new UserData
                        {
                            FirstName = userInformation[0],
                            LastName = userInformation[1],
                            Address = userInformation[2],
                            City = userInformation[3],
                            State = userInformation[4],
                            ZipCode = userInformation[5],
                            Email = userInformation[6],
                            Phone = userInformation[7]
                        };
                        udContext.Add(userdat);
                    context.SaveChanges();
                    CompletedApp cApp = new CompletedApp { UserDataId = userdat.UserDataId, Date = DateTime.Today.ToShortDateString(), Answers = appString, Status = "pending_review", Position = positions, Comments = "" };
                    appcontext.Add(cApp);
                    context.SaveChanges();
                    }
                }
                catch (Exception)
                {
                    results = -1;
                }
            }
            else
            {
                results = 1;
            }
            return View(results);
        }