Skip to content

Commit

Permalink
fix string-list components, including a hack for polls, sometimes asp…
Browse files Browse the repository at this point in the history
….net is a real drag
  • Loading branch information
adelikat committed Jun 4, 2024
1 parent 45a1afb commit 86a9381
Show file tree
Hide file tree
Showing 7 changed files with 13 additions and 12 deletions.
2 changes: 1 addition & 1 deletion TASVideos/Extensions/ViewDataDictionaryExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -99,5 +99,5 @@ public static void UseStringList(this ViewDataDictionary viewData)
=> viewData["use-string-list"] = true;

public static bool UsesStringList(this ViewDataDictionary viewData)
=> viewData["use-backup-text"] is not null;
=> viewData["use-string-list"] is not null;
}
5 changes: 1 addition & 4 deletions TASVideos/Pages/Forum/Subforum/Edit.cshtml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -92,10 +92,7 @@ private async Task Initialize()
AvailableCategories = await db.ForumCategories.ToDropdownList();
}

private async Task<bool> CanBeDeleted()
{
return !await db.ForumTopics.AnyAsync(t => t.ForumId == Id);
}
private async Task<bool> CanBeDeleted() => !await db.ForumTopics.AnyAsync(t => t.ForumId == Id);

public class ForumEdit
{
Expand Down
1 change: 1 addition & 0 deletions TASVideos/Pages/Forum/Topics/AddEditPoll.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
@{
var mode = Model.PollId.HasValue ? "Edit" : "Add";
ViewData.SetTitle($"{mode}ing Poll for Topic {Model.TopicTitle}");
ViewData.UseStringList(); // Hack for now, partial views use a different viewDataDictionary, so the component will not work
}

<partial Name="_ForumHeader" />
Expand Down
3 changes: 0 additions & 3 deletions TASVideos/Pages/Forum/Topics/AddEditPoll.cshtml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -121,14 +121,11 @@ public class PollCreate
[StringLength(200, MinimumLength = 8)]
public string? Question { get; init; }

[Display(Name = "Days to Run for", Description = "0 or empty for a never-ending poll")]
[Range(0, 365)]
public int? DaysOpen { get; init; }

[Display(Name = "Allow Multiple Selections")]
public bool MultiSelect { get; init; }

[Display(Name = "Options")]
public List<string> PollOptions { get; init; } = ["", ""];

public bool IsValid =>
Expand Down
1 change: 1 addition & 0 deletions TASVideos/Pages/Forum/Topics/Create.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
@model CreateModel
@{
ViewData.SetTitle($"Creating topic for: {Model.ForumName}");
ViewData.UseStringList(); // Hack for now, partial views use a different viewDataDictionary, so the component will not work
}
@section PageTitle {
Creating topic for: <a asp-page="/Forum/Subforum/Index" asp-route-id="@Model.ForumId">@Model.ForumName</a>
Expand Down
8 changes: 4 additions & 4 deletions TASVideos/Pages/Forum/Topics/_PollCreate.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@
<span asp-validation-for="Question" class="text-danger"></span>
</fieldset>
<fieldset>
<label asp-for="DaysOpen"></label>
<label asp-for="DaysOpen">Days to Run for</label>
<input asp-for="DaysOpen" type="number" min="0" max="365" class="form-control" />
<div>@Html.DescriptionFor(m => m.DaysOpen)</div>
<div>0 or empty for a never-ending poll</div>
<span asp-validation-for="DaysOpen" class="text-danger"></span>
</fieldset>
<fieldset condition="!Model.HasVotes">
<label asp-for="PollOptions"></label>
<label asp-for="PollOptions">Options</label>
<string-list asp-for="PollOptions" />
<span asp-validation-for="PollOptions" class="text-danger"></span>
</fieldset>
Expand All @@ -29,7 +29,7 @@
</fieldset>
<fieldset disable="true">
<div class="form-check">
<label asp-for="MultiSelect" class="form-check-label"></label>
<label asp-for="MultiSelect" class="form-check-label">Allow Multiple Selections</label>
<input disable="@Model.HasVotes" asp-for="MultiSelect" type="checkbox" class="form-check-input" />
</div>
<span asp-validation-for="MultiSelect" class="text-danger"></span>
Expand Down
5 changes: 5 additions & 0 deletions TASVideos/TagHelpers/StringListTagHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,14 @@ public class StringListTagHelper : TagHelper
{
public ModelExpression AspFor { get; set; } = null!;

[HtmlAttributeNotBound]
[ViewContext]
public ViewContext ViewContext { get; set; } = new();

public override void Process(TagHelperContext context, TagHelperOutput output)
{
ValidateExpression();
ViewContext.ViewData.UseStringList();
output.TagMode = TagMode.StartTagAndEndTag;
output.TagName = "div";

Expand Down

0 comments on commit 86a9381

Please sign in to comment.