public ActionResult _EditFunctionState(PD_FunctionState_ViewModel model)
        {
            var _p = _db.PD_Processes.Find(model.PD_Process_Id);
            if (User.Identity.Name.Equals(_p.LockedBy))
            {
                PD_FunctionState state = _db.PD_FunctionStates.Find(model.PD_Process_Id, model.PD_Subject_Id, model.Id);
                state.Name = model.Name;
                state.EndState = model.EndState;
                state.StartState = model.StartState;
                _db.SaveChanges();
                if (model.AvailableEditableParameters == null) return RedirectToAction("ViewSubject", new { processid = model.PD_Process_Id, subjectid = model.PD_Subject_Id, edit = 1 });
                model.SelectedEditableParameters = new List<string>();
                foreach (var i in model.AvailableEditableParameters)
                {
                    if (i.Selected == true) model.SelectedEditableParameters.Add(i.Value);
                }
                model.SelectedReadableParameters = new List<string>();
                foreach (var i in model.AvailableReadableParameters)
                {
                    if (i.Selected == true) model.SelectedReadableParameters.Add(i.Text);
                }



                state.ReadableParameters = new PersistableStringCollection();
                if (model.SelectedReadableParameters != null)
                {
                    model.SelectedReadableParameters.ForEach(item => state.ReadableParameters.Add(item));
                }


                state.EditableParameters = new PersistableStringCollection();
                if (model.SelectedEditableParameters != null)
                {
                    model.SelectedEditableParameters.ForEach(item => state.EditableParameters.Add(item));
                }


                _db.SaveChanges();

                return RedirectToAction("ViewSubject", new { processid = model.PD_Process_Id, subjectid = model.PD_Subject_Id, edit = 1 });
            }
            else
            {
                return null;
            }
        }
        public ActionResult _EditFunctionState(int processid, int subjectid, int stateid, int edit)
        {
            var state = _db.PD_FunctionStates.Find(processid, subjectid, stateid);
            PD_FunctionState_ViewModel Model = new PD_FunctionState_ViewModel() { PD_Process_Id = state.PD_Process_Id, PD_Subject_Id = state.PD_Subject_Id, Id = state.Id, Name = state.Name, EndState = state.EndState , StartState = state.StartState};

            List<SelectListItem> AvailableParameters = new List<SelectListItem>();
            List<SelectListItem> SelectedEditableParameters2 = new List<SelectListItem>();
            List<SelectListItem> SelectedReadableParameters2 = new List<SelectListItem>();

            var parameters = _db.PD_Subjects.Find(processid, subjectid).GlobalParameters;
            List<String> parametersMod = new List<String>();
            foreach (var i in parameters)
            {
                if(!parametersMod.Contains(i))parametersMod.Add(i);
            }
            foreach (var i in parametersMod)
            {                
                AvailableParameters.Add(new SelectListItem() { Value = i, Text = i });

                var eNewSelectItem = new SelectListItem() { Value = i, Text = i, Selected = false };
                var rNewSelectItem = new SelectListItem() { Value = i, Text = i, Selected = false };
                AvailableParameters.Add(new SelectListItem() { Value = i, Text = i, Selected = true });
                foreach (var f in state.ReadableParameters.ToList()) { if (f == i) rNewSelectItem.Selected = true; }
                foreach (var g in state.EditableParameters.ToList()) { if (g == i) eNewSelectItem.Selected = true; }
                SelectedReadableParameters2.Add(rNewSelectItem);
                SelectedEditableParameters2.Add(eNewSelectItem);                
            }

            Model.AvailableEditableParameters = SelectedEditableParameters2;
            Model.AvailableReadableParameters = SelectedReadableParameters2;

            ViewBag.Edit = edit;
            return PartialView(Model);
        }