Пример #1
0
        public override object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
        {
            ModelStateDictionary mState = bindingContext.ModelState;

            System.Collections.Specialized.NameValueCollection form = controllerContext.HttpContext.Request.Form;

            RoleView roleViewModel = new RoleView();

            roleViewModel.Name = form["Name"];

            if (form["HasAvailableFunctions"] != null)
            {
                roleViewModel.HasAvailableFunctions = bool.Parse(form["HasAvailableFunctions"]);
            }

            if (roleViewModel.HasAvailableFunctions == false)
            {
                mState.AddModelError("HasAvailableFunctions", "Please select at least one available function.");
            }

            if (roleViewModel.AvailableFunctions == null)
            {
                roleViewModel.AvailableFunctions = new List <CheckBoxListInfo>();
            }

            if (form.AllKeys.Length > 2)
            {
                for (int i = 2; i < form.AllKeys.Length; i++)
                {
                    if (form.AllKeys[i].Substring(0, form.AllKeys[i].IndexOf('[')).Contains("AvailableFunctions"))
                    {
                        CheckBoxListInfo chk = new CheckBoxListInfo();
                        chk.Value = form[form.AllKeys[i]];



                        roleViewModel.AvailableFunctions.Add(chk);
                    }
                }
            }


            return(roleViewModel);
        }
Пример #2
0
        public static List <CheckBoxListInfo> ToCheckBoxList <T>(this List <T> obj)
        {
            var type = obj.GetType().GetGenericArguments()[0];

            if (!type.IsEnum)
            {
                throw new InvalidOperationException(type.Name + " is not a list of enumerations");
            }
            var list   = new List <CheckBoxListInfo>();
            var values = Enum.GetValues(type);

            foreach (var entry in values)
            {
                bool isChecked = false;
                var  e         = (T)entry;
                if (obj.Contains <T>(e))
                {
                    isChecked = true;
                }
                var info = new CheckBoxListInfo(((Enum)entry).ToInt().ToString(), ((Enum)entry).ToDescriptionString(), isChecked);
                list.Add(info);
            }
            return(list);
        }
        public override object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
        {
            ModelStateDictionary mState = bindingContext.ModelState;

            System.Collections.Specialized.NameValueCollection form = controllerContext.HttpContext.Request.Form;

            CoachManageTeamEventsView coachManageTeamsEventsView = new CoachManageTeamEventsView();

            if (form["Event.Name"] == "")
            {
                mState.AddModelError("Name", "Name is required");
            }

            if (form["Event.ScheduledDate"] == "")
            {
                mState.AddModelError("ScheduledDate", "Scheduled date is required");
            }

            if (form["Event.Description"] == "")
            {
                mState.AddModelError("Description", "Description is required");
            }

            if (form["Event.Location"] == "")
            {
                mState.AddModelError("Location", "Location is required");
            }

            if (form["HasSelectedTeam"] != null)
            {
                coachManageTeamsEventsView.HasSelectedTeam = bool.Parse(form["HasSelectedTeam"]);
            }

            if (coachManageTeamsEventsView.HasSelectedTeam == false)
            {
                mState.AddModelError("HasSelectedTeam", "Please select at least one available team.");
            }

            if (mState.Count == 0)
            {
                coachManageTeamsEventsView.Event               = new Event(Guid.NewGuid());
                coachManageTeamsEventsView.Event.Name          = form["Event.Name"];
                coachManageTeamsEventsView.Event.Location      = form["Event.Location"];
                coachManageTeamsEventsView.Event.ScheduledDate = DateTime.Parse(form["Event.ScheduledDate"]);
                coachManageTeamsEventsView.Event.Description   = form["Event.Description"];
                coachManageTeamsEventsView.SendEmailToPlayers  = form["SendEmailToPlayers"].ToString().Equals("false") ? false : true;


                if (form.AllKeys.Length > 5)
                {
                    coachManageTeamsEventsView.TeamsToNotify = new List <CheckBoxListInfo>();

                    for (int i = 3; i < form.AllKeys.Length - 3; i++)
                    {
                        if (form.AllKeys[i].Substring(0, form.AllKeys[i].IndexOf('[')).Contains("Teams"))
                        {
                            CheckBoxListInfo chk = new CheckBoxListInfo();
                            chk.Value = form[form.AllKeys[i]];

                            coachManageTeamsEventsView.TeamsToNotify.Add(chk);
                        }
                    }
                }
            }

            return(coachManageTeamsEventsView);
        }