Пример #1
0
        public async Task<ActionResult> AddReusablePartToPOST(string id)
        {
            if (!await _authorizationService.AuthorizeAsync(User, Permissions.EditContentTypes))
                return Unauthorized();

            var typeViewModel = _contentDefinitionService.GetType(id);

            if (typeViewModel == null)
                return NotFound();

            var viewModel = new AddReusablePartViewModel();
            if (!await TryUpdateModelAsync(viewModel))
            {
                return await AddReusablePartTo(id);
            }

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

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

            var partToAdd = viewModel.SelectedPartName;

            _contentDefinitionService.AddReusablePartToType(viewModel.Name, viewModel.DisplayName, viewModel.Description, partToAdd, typeViewModel.Name);
            _notifier.Success(T["The \"{0}\" part has been added.", partToAdd]);

            if (!ModelState.IsValid)
            {
                _session.Cancel();
                return await AddReusablePartTo(id);
            }

            return RedirectToAction("Edit", new { id });
        }
Пример #2
0
        public async Task<ActionResult> AddReusablePartTo(string id)
        {
            if (!await _authorizationService.AuthorizeAsync(User, Permissions.EditContentTypes))
            {
                return Unauthorized();
            }

            var typeViewModel = _contentDefinitionService.GetType(id);

            if (typeViewModel == null)
                return NotFound();

            var reusableParts = _contentDefinitionService.GetParts(metadataPartsOnly: false)
                    .Where(cpd =>
                        cpd.Settings.ToObject<ContentPartSettings>().Attachable &&
                        cpd.Settings.ToObject<ContentPartSettings>().Reusable);

            var viewModel = new AddReusablePartViewModel
            {
                Type = typeViewModel,
                PartSelections = reusableParts
                    .Select(cpd => new PartSelectionViewModel { PartName = cpd.Name, PartDisplayName = cpd.DisplayName, PartDescription = cpd.Description })
                    .ToList(),
                SelectedPartName = reusableParts.FirstOrDefault()?.Name
            };

            return View(viewModel);
        }