public ActionResult Create(ModulesViewModel _model)
        {
            if (!ModelState.IsValid)
            {
                _model.DataTypeList = GetDataTypeList();
                return(View(_model));
            }

            if (_model.Fields.Count(f => f.ShowInListMode) == 0)
            {
                TempData["Message"] = "Pelo menos um dos campos deve ser visível na listagem.";
                _model.DataTypeList = GetDataTypeList();
                return(View(_model));
            }

            // retrieve the current account name and associate with the newly created module
            var acctName = RouteData.Values["account"] as string;

            if (String.IsNullOrWhiteSpace(acctName))
            {
                TempData["Message"] = "Conta não encontrada.";
                return(View(_model));
            }

            Module newModule = new Module()
            {
                DisplayName = _model.DisplayName,
                ModuleName  = _model.DisplayName.EscapeName(),
                User        = ModRepository.GetUserByName(Membership.GetUser().UserName), // associate the module with the current logged in user
                Account     = ModRepository.GetAccountByName(acctName),
                ModuleType  = _model.Type
            };

            _model.Fields.ToList().ForEach(f => {
                var newField = new Field {
                    DisplayName    = f.FieldDisplayName,
                    FieldName      = f.FieldDisplayName.EscapeName(),
                    FieldDataType  = ModRepository.GetDataTypeByName(f.DataType),
                    IsReadOnly     = f.IsReadOnly,
                    IsRequired     = f.IsRequired,
                    ShowInListMode = f.ShowInListMode
                };

                if (f.Metadata != null)
                {
                    newField.AddMetadata(f.Metadata);
                }
                newModule.Fields.Add(newField);
            });

            ModRepository.AddModule(newModule);

            TempData["Message"] = "Módulo salvo com sucesso.";
            return(RedirectToAction("Index", "Home"));
        }