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)); }
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); if (!ModelState.IsValid) { _session.Cancel(); return(await AddReusablePartTo(id)); } else { _notifier.Success(T["The \"{0}\" part has been added.", partToAdd]); } return(RedirectToAction("Edit", new { id })); }
public async Task <ActionResult> AddReusablePartToPOST(string id) { if (!await _authorizationService.AuthorizeAsync(User, Permissions.EditContentTypes)) { return(Forbid()); } var typeViewModel = _contentDefinitionService.LoadType(id); if (typeViewModel == null) { return(NotFound()); } var viewModel = new AddReusablePartViewModel(); if (!await TryUpdateModelAsync(viewModel)) { return(await AddReusablePartTo(id)); } viewModel.DisplayName = viewModel.DisplayName?.Trim() ?? String.Empty; viewModel.Name = viewModel.Name ?? String.Empty; if (String.IsNullOrWhiteSpace(viewModel.DisplayName)) { ModelState.AddModelError("DisplayName", S["The Display Name can't be empty."]); } if (typeViewModel.TypeDefinition.Parts.Any(f => String.Equals(f.DisplayName().Trim(), viewModel.DisplayName.Trim(), StringComparison.OrdinalIgnoreCase))) { ModelState.AddModelError("DisplayName", S["A part with the same Display 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 (String.IsNullOrWhiteSpace(viewModel.Name)) { ModelState.AddModelError("Name", S["The Technical Name can't be empty."]); } if (typeViewModel.TypeDefinition.Parts.Any(f => String.Equals(f.Name.Trim(), viewModel.Name.Trim(), StringComparison.OrdinalIgnoreCase))) { ModelState.AddModelError("Name", S["A part with the same Technical Name already exists."]); } if (!ModelState.IsValid) { _documentStore.Cancel(); return(await AddReusablePartTo(id)); } var partToAdd = viewModel.SelectedPartName; _contentDefinitionService.AddReusablePartToType(viewModel.Name, viewModel.DisplayName, viewModel.Description, partToAdd, typeViewModel.Name); _notifier.Success(H["The \"{0}\" part has been added.", partToAdd]); return(RedirectToAction("Edit", new { id })); }