Esempio n. 1
0
        public async Task <ActionResult> CreatePartPOST(CreatePartViewModel viewModel)
        {
            if (!await _authorizationService.AuthorizeAsync(User, Permissions.EditContentTypes))
            {
                return(Forbid());
            }

            viewModel.Name = viewModel.Name ?? String.Empty;

            if (String.IsNullOrWhiteSpace(viewModel.Name))
            {
                ModelState.AddModelError("Name", S["The Technical Name can't be empty."]);
            }

            if (_contentDefinitionService.LoadParts(false).Any(p => String.Equals(p.Name.Trim(), viewModel.Name.Trim(), StringComparison.OrdinalIgnoreCase)))
            {
                ModelState.AddModelError("Name", S["A part with the same Technical Name already exists."]);
            }

            if (!String.IsNullOrWhiteSpace(viewModel.Name) && !viewModel.Name[0].IsLetter())
            {
                ModelState.AddModelError("Name", S["The Technical Name must start with a letter."]);
            }

            if (!String.Equals(viewModel.Name, viewModel.Name.ToSafeName(), StringComparison.OrdinalIgnoreCase))
            {
                ModelState.AddModelError("Name", S["The Technical Name contains invalid characters."]);
            }

            if (viewModel.Name.IsReservedContentName())
            {
                ModelState.AddModelError("Name", S["The Technical Name is reserved for internal use."]);
            }

            if (!ModelState.IsValid)
            {
                return(View(viewModel));
            }

            var partViewModel = _contentDefinitionService.AddPart(viewModel);

            if (partViewModel == null)
            {
                await _notifier.InformationAsync(H["The content part could not be created."]);

                return(View(viewModel));
            }

            await _notifier.SuccessAsync(H["The \"{0}\" content part has been created.", partViewModel.Name]);

            return(RedirectToAction(nameof(EditPart), new { id = partViewModel.Name }));
        }