public override async Task <IViewProviderResult> BuildEditAsync(Topic entity, IViewProviderContext context) { // We always need an entity if (entity == null) { return(default(IViewProviderResult)); } // Set isPrivate flag var isPrivate = entity.Id > 0 && entity.IsPrivate; // Ensures we persist selection between post backs if (context.Controller.HttpContext.Request.Method == "POST") { foreach (string key in context.Controller.HttpContext.Request.Form.Keys) { if (key.StartsWith(TopicViewProvider.HtmlName)) { var values = context.Controller.HttpContext.Request.Form[key]; foreach (var value in values) { if (value.IndexOf("private", StringComparison.OrdinalIgnoreCase) >= 0) { isPrivate = true; } if (value.IndexOf("public", StringComparison.OrdinalIgnoreCase) >= 0) { isPrivate = false; } } } } } // Build dropdown view model var selectedValue = isPrivate ? "private" : "public"; var viewModel = new VisibilityDropDownViewModel() { HtmlName = HtmlName, SelectedValue = selectedValue, DropDown = new SelectDropDown() { Title = "Visibility", InnerCssClass = "d-block", Items = new List <SelectDropDownItem>() { new SelectDropDownItem() { Text = "Public", Description = "This topic will be visible to everyone. Chose this option if your sharing public information and don't mind public replies", Value = "public", Checked = selectedValue == "public" ? true : false, Permission = entity.Id == 0 ? Permissions.DiscussPrivateCreatePublic : Permissions.DiscussPrivateToPublic }, new SelectDropDownItem() { Text = "Private", Description = "This topic will only be visible to you and our team. Choose this option if your sharing private information.", Value = "private", Checked = selectedValue == "private" ? true : false, Permission = entity.Id == 0 ? Permissions.DiscussPrivateCreatePrivate : Permissions.DiscussPrivateToPrivate } } } }; // For new entities adjust model to ensure the first appropriate // option is selected based on our current permissions if (entity.Id == 0) { await viewModel.AdjustInitiallySelected(_authorizationService, context.Controller.User); } // Add dropdown view model to context for use within navigation provider context.Controller.HttpContext.Items[typeof(VisibilityDropDownViewModel)] = viewModel; // No view modifications return(default(IViewProviderResult)); }
public override async Task <IViewProviderResult> BuildEditAsync(Article entity, IViewProviderContext context) { // We always need an entity if (entity == null) { return(default(IViewProviderResult)); } // Initial values var isHidden = entity.IsHidden; var isPrivate = entity.IsPrivate; if (entity.Id == 0) { isHidden = false; isPrivate = true; } // Ensures we persist selection between post backs if (context.Controller.HttpContext.Request.Method == "POST") { foreach (string key in context.Controller.HttpContext.Request.Form.Keys) { if (key.StartsWith(ArticleViewProvider.HtmlName)) { var values = context.Controller.HttpContext.Request.Form[key]; foreach (var value in values) { if (value.IndexOf("private", StringComparison.OrdinalIgnoreCase) >= 0) { isPrivate = true; } if (value.IndexOf("public", StringComparison.OrdinalIgnoreCase) >= 0) { isPrivate = false; } } } } } // Build dropdown view model var selectedValue = isHidden ? "hidden" : (isPrivate ? "private" : "public"); var viewModel = new VisibilityDropDownViewModel() { HtmlName = ArticleViewProvider.HtmlName, SelectedValue = selectedValue, DropDown = new SelectDropDown() { Title = "Visibility", InnerCssClass = "d-block", Items = new List <SelectDropDownItem>() { new SelectDropDownItem() { Text = "Private", Description = "The article will only be visible to you and those with permission to view private articles. Choose this option whilst your authoring your article.", Value = "private", Checked = selectedValue == "private" ? true : false, Permission = entity.Id == 0 ? Permissions.ArticlesDraftCreatePrivate : Permissions.ArticlesDraftToPrivate }, new SelectDropDownItem() { Text = "Hidden", Description = "The article will only be visible to those with permission to view hidden articles. Choose this option once your article is ready for review.", Value = "hidden", Checked = selectedValue == "hidden" ? true : false, Permission = entity.Id == 0 ? Permissions.ArticlesDraftCreateHidden : Permissions.ArticlesDraftToHidden }, new SelectDropDownItem() { Text = "Public", Description = "The article will be visible to everyone. Chose this option once your ready to publish to the world.", Value = "public", Checked = selectedValue == "public" ? true : false, Permission = entity.Id == 0 ? Permissions.ArticlesDraftCreatePublic : Permissions.ArticlesDraftToPublic } } } }; // For new entities adjust model to ensure the first appropriate // option is selected based on our current permissions if (entity.Id == 0) { await viewModel.AdjustInitiallySelected(_authorizationService, context.Controller.User); } // Add dropdown view model to context for use within navigation provider context.Controller.HttpContext.Items[typeof(VisibilityDropDownViewModel)] = viewModel; // No view modifications return(default(IViewProviderResult)); }