public async Task <ActionResult> AddFieldToPOST(AddFieldViewModel viewModel, string id) { if (!await _authorizationService.AuthorizeAsync(User, Permissions.EditContentTypes)) { return(Unauthorized()); } var partViewModel = _contentDefinitionService.GetPart(id); if (partViewModel == null) { return(NotFound()); } var partDefinition = partViewModel.PartDefinition; 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 name can't be empty."]); } if (String.IsNullOrWhiteSpace(viewModel.Name)) { ModelState.AddModelError("Name", S["The Technical Name can't be empty."]); } if (partDefinition.Fields.Any(t => String.Equals(t.Name.Trim(), viewModel.Name.Trim(), StringComparison.OrdinalIgnoreCase))) { ModelState.AddModelError("Name", S["A field with the same 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 (partDefinition.Fields.Any(t => String.Equals(t.DisplayName().Trim(), Convert.ToString(viewModel.DisplayName).Trim(), StringComparison.OrdinalIgnoreCase))) { ModelState.AddModelError("DisplayName", S["A field with the same Display Name already exists."]); } if (!ModelState.IsValid) { viewModel.Part = partDefinition; viewModel.Fields = _contentDefinitionService.GetFields().Select(x => x.Name).OrderBy(x => x).ToList(); _session.Cancel(); return(View(viewModel)); } _contentDefinitionService.AddFieldToPart(viewModel.Name, viewModel.DisplayName, viewModel.FieldTypeName, partDefinition.Name); _notifier.Success(T["The field \"{0}\" has been added.", viewModel.DisplayName]); return(RedirectToAction("EditField", new { id, viewModel.Name })); }
public async Task <ActionResult> AddFieldToPOST(AddFieldViewModel viewModel, string id) { if (!await _authorizationService.AuthorizeAsync(User, Permissions.EditContentTypes)) { return(Unauthorized()); } var partViewModel = _contentDefinitionService.GetPart(id); var typeViewModel = _contentDefinitionService.GetType(id); if (partViewModel == null) { // id passed in might be that of a type w/ no implicit field if (typeViewModel != null) { partViewModel = new EditPartViewModel { Name = typeViewModel.Name }; _contentDefinitionService.AddPart(new CreatePartViewModel { Name = partViewModel.Name }); _contentDefinitionService.AddPartToType(partViewModel.Name, typeViewModel.Name); } else { return(NotFound()); } } viewModel.DisplayName = viewModel.DisplayName ?? String.Empty; viewModel.DisplayName = viewModel.DisplayName.Trim(); viewModel.Name = viewModel.Name ?? String.Empty; if (String.IsNullOrWhiteSpace(viewModel.DisplayName)) { ModelState.AddModelError("DisplayName", S["The Display Name name can't be empty."]); } if (String.IsNullOrWhiteSpace(viewModel.Name)) { ModelState.AddModelError("Name", S["The Technical Name can't be empty."]); } if (_contentDefinitionService.GetPart(partViewModel.Name).PartDefinition.Fields.Any(t => String.Equals(t.Name.Trim(), viewModel.Name.Trim(), StringComparison.OrdinalIgnoreCase))) { ModelState.AddModelError("Name", S["A field with the same 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 (_contentDefinitionService.GetPart(partViewModel.Name).PartDefinition.Fields.Any(t => String.Equals(t.DisplayName.Trim(), Convert.ToString(viewModel.DisplayName).Trim(), StringComparison.OrdinalIgnoreCase))) { ModelState.AddModelError("DisplayName", S["A field with the same Display Name already exists."]); } if (!ModelState.IsValid) { viewModel.Part = partViewModel; viewModel.Fields = _contentDefinitionService.GetFields(); _session.Cancel(); return(View(viewModel)); } try { _contentDefinitionService.AddFieldToPart(viewModel.Name, viewModel.DisplayName, viewModel.FieldTypeName, partViewModel.Name); } catch { //Services.Notifier.Information(T("The \"{0}\" field was not added. {1}", viewModel.DisplayName, ex.Message)); _session.Cancel(); return(await AddFieldTo(id)); } _notifier.Success(T["The \"{0}\" field has been added.", viewModel.DisplayName]); if (typeViewModel != null) { return(RedirectToAction("Edit", new { id })); } return(RedirectToAction("EditPart", new { id })); }
public async Task <ActionResult> AddFieldToPOST(AddFieldViewModel viewModel, string id, string returnUrl = null) { if (!await _authorizationService.AuthorizeAsync(User, Permissions.EditContentTypes)) { return(Forbid()); } var partViewModel = _contentDefinitionService.LoadPart(id); if (partViewModel == null) { return(NotFound()); } var partDefinition = partViewModel.PartDefinition; 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 (partDefinition.Fields.Any(f => String.Equals(f.DisplayName().Trim(), viewModel.DisplayName.Trim(), StringComparison.OrdinalIgnoreCase))) { ModelState.AddModelError("DisplayName", S["A field with the same Display Name already exists."]); } if (String.IsNullOrWhiteSpace(viewModel.Name)) { ModelState.AddModelError("Name", S["The Technical Name can't be empty."]); } if (partDefinition.Fields.Any(f => String.Equals(f.Name.Trim(), viewModel.Name.Trim(), StringComparison.OrdinalIgnoreCase))) { ModelState.AddModelError("Name", S["A field 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 (!ModelState.IsValid) { viewModel.Part = partDefinition; viewModel.Fields = _contentDefinitionService.GetFields().Select(x => x.Name).OrderBy(x => x).ToList(); await _documentStore.CancelAsync(); ViewData["ReturnUrl"] = returnUrl; return(View(viewModel)); } _contentDefinitionService.AddFieldToPart(viewModel.Name, viewModel.DisplayName, viewModel.FieldTypeName, partDefinition.Name); await _notifier.SuccessAsync(H["The field \"{0}\" has been added.", viewModel.DisplayName]); if (!String.IsNullOrEmpty(returnUrl) && Url.IsLocalUrl(returnUrl)) { return(this.Redirect(returnUrl, true)); } else { return(RedirectToAction(nameof(EditField), new { id, viewModel.Name })); } }
public ActionResult AddFieldToPOST(AddFieldViewModel viewModel, string id) { if (!Services.Authorizer.Authorize(Permissions.EditContentTypes, T("Not allowed to edit a content part."))) { return(new HttpUnauthorizedResult()); } var partViewModel = _contentDefinitionService.GetPart(id); var typeViewModel = _contentDefinitionService.GetType(id); if (partViewModel == null) { // id passed in might be that of a type w/ no implicit field if (typeViewModel != null) { partViewModel = new EditPartViewModel { Name = typeViewModel.Name }; _contentDefinitionService.AddPart(new CreatePartViewModel { Name = partViewModel.Name }); _contentDefinitionService.AddPartToType(partViewModel.Name, typeViewModel.Name); } else { return(HttpNotFound()); } } viewModel.DisplayName = viewModel.DisplayName ?? String.Empty; viewModel.DisplayName = viewModel.DisplayName.Trim(); viewModel.Name = viewModel.Name ?? String.Empty; if (String.IsNullOrWhiteSpace(viewModel.DisplayName)) { ModelState.AddModelError("DisplayName", T("显示名称不能为空.").ToString()); } if (String.IsNullOrWhiteSpace(viewModel.Name)) { ModelState.AddModelError("Name", T("技术名称不能为空.").ToString()); } if (_contentDefinitionService.GetPart(partViewModel.Name).Fields.Any(t => String.Equals(t.Name.Trim(), viewModel.Name.Trim(), StringComparison.OrdinalIgnoreCase))) { ModelState.AddModelError("Name", T("该字段已存在.").ToString()); } if (!String.IsNullOrWhiteSpace(viewModel.Name) && !viewModel.Name[0].IsLetter()) { ModelState.AddModelError("Name", T("技术名称必须以字母开头.").ToString()); } if (!String.Equals(viewModel.Name, viewModel.Name.ToSafeName(), StringComparison.OrdinalIgnoreCase)) { ModelState.AddModelError("Name", T("技术名称包含非法字符.").ToString()); } if (_contentDefinitionService.GetPart(partViewModel.Name).Fields.Any(t => String.Equals(t.DisplayName.Trim(), Convert.ToString(viewModel.DisplayName).Trim(), StringComparison.OrdinalIgnoreCase))) { ModelState.AddModelError("DisplayName", T("显示名称已存在.").ToString()); } if (!ModelState.IsValid) { viewModel.Part = partViewModel; viewModel.Fields = _contentDefinitionService.GetFields(); Services.TransactionManager.Cancel(); return(View(viewModel)); } try { _contentDefinitionService.AddFieldToPart(viewModel.Name, viewModel.DisplayName, viewModel.FieldTypeName, partViewModel.Name); } catch (Exception ex) { Services.Notifier.Information(T("字段 \"{0}\" 添加失败. {1}", viewModel.DisplayName, ex.Message)); Services.TransactionManager.Cancel(); return(AddFieldTo(id)); } Services.Notifier.Information(T("字段 \"{0}\" 添加成功.", viewModel.DisplayName)); if (typeViewModel != null) { return(RedirectToAction("Edit", new { id })); } return(RedirectToAction("EditPart", new { id })); }
public ActionResult AddFieldToPOST(string id) { if (!Services.Authorizer.Authorize(Permissions.EditContentTypes, T("Not allowed to edit a content part."))) { return(new HttpUnauthorizedResult()); } var partViewModel = _contentDefinitionService.GetPart(id); var typeViewModel = _contentDefinitionService.GetType(id); if (partViewModel == null) { //id passed in might be that of a type w/ no implicit field if (typeViewModel != null) { partViewModel = new EditPartViewModel { Name = typeViewModel.Name }; _contentDefinitionService.AddPart(new CreatePartViewModel { Name = partViewModel.Name }); _contentDefinitionService.AddPartToType(partViewModel.Name, typeViewModel.Name); } else { return(HttpNotFound()); } } var viewModel = new AddFieldViewModel(); if (!TryUpdateModel(viewModel)) { Services.TransactionManager.Cancel(); return(AddFieldTo(id)); } try { _contentDefinitionService.AddFieldToPart(viewModel.DisplayName, viewModel.FieldTypeName, partViewModel.Name); } catch (Exception ex) { Services.Notifier.Information(T("The \"{0}\" field was not added. {1}", viewModel.DisplayName, ex.Message)); Services.TransactionManager.Cancel(); return(AddFieldTo(id)); } if (!ModelState.IsValid) { Services.TransactionManager.Cancel(); return(AddFieldTo(id)); } Services.Notifier.Information(T("The \"{0}\" field has been added.", viewModel.DisplayName)); if (typeViewModel != null) { return(RedirectToAction("Edit", new { id })); } return(RedirectToAction("EditPart", new { id })); }