У меня есть следующий код, который я хочу упростить:
using System;
using System.Collections;
using System.Collections.Generic;
using Microsoft.AspNetCore.Mvc.Rendering;
using Microsoft.AspNetCore.Mvc.TagHelpers;
using Microsoft.AspNetCore.Mvc.ViewFeatures;
using Microsoft.AspNetCore.Razor.TagHelpers;
namespace Sandbox.Feature.PermissionManagement
{
[HtmlTargetElement("simpleCheckboxList")]
public class TypedCheckboxOptionHelper : TagHelper
{
private readonly IHtmlGenerator _htmlGenerator;
public TypedCheckboxOptionHelper(IHtmlGenerator htmlGenerator)
{
_htmlGenerator = htmlGenerator;
}
[HtmlAttributeName("source")]
public ModelExpression Source { get; set; }
[HtmlAttributeName("title")]
public string Title { get; set; }
[ViewContext]
[HtmlAttributeNotBound]
public ViewContext ViewContext { get; set; }
/// <inheritdoc />
public override void Process(TagHelperContext context, TagHelperOutput output)
{
/**
*
<label class="checkbox-option">@Model.title</label>
@for (var index = 0; index < Model.data.Count; index++)
{
<div>
<input type="checkbox" asp-for="@Model.data[index].Checked"/>
<label asp-for="@Model.data[index].Checked">@Model.data[index].Description</label>
<input type="hidden" asp-for="@Model.data[index].Value"/>
<input type="hidden" asp-for="@Model.data[index].Description"/>
</div>
}
<span asp-validation-for="@Model.data" class="text-danger"></span>
*/
var model = Source.Model as IEnumerable;
if (model == null)
return;
var modelExplorer = Source.ModelExplorer;
output.Content.AppendHtml(@"<label class=""checkbox-option"">" + Title + @"</label>");
var index = 0;
foreach (var item in model)
{
if (item is ICheckboxOption option)
{
// var tagHelper = new InputTagHelper(_htmlGenerator)
// {
// For = new ModelExpression($"{Source.Name}[" + index + "].Checked", modelExplorer),
// InputTypeName = "checkbox",
// ViewContext = ViewContext,
// Name = Source.Name
// };
//
// var attributes = new TagHelperAttributeList
// {
// { "name", $"{Source.Name}[" + index + "].Checked" },
// { "type", "checkbox" },
// };
// tagHelper.Process(new TagHelperContext("input", attributes, new Dictionary<object, object>(), Guid.NewGuid().ToString()), output);
var value = _htmlGenerator.GenerateCheckBox(
ViewContext,
Source.ModelExplorer,
$"{Source.Name}[" + index + "].Checked",
option.Checked,
new []{"type","checkbox"});
output.Content.AppendHtml("<div>");
output.Content.AppendHtml(value);
output.Content.AppendHtml("</div>");
}
}
// output.Content.Append("@for (var index = 0; index < Model.data.Count; index++)");
// output.Content.Append("{");
// output.Content.Append(@"<div>");
// output.Content.Append(@"<input type=""checkbox"" asp-for=""@Model.data[index].Checked""/>");
// output.Content.Append(@"<label asp-for=""@Model.data[index].Checked"">@Model.data[index].Description</label>");
// output.Content.Append(@"<input type=""hidden"" asp-for=""@Model.data[index].Value""/>");
// output.Content.Append(@"<input type=""hidden"" asp-for=""@Model.data[index].Description""/>");
// output.Content.Append(@"</div>");
// output.Content.Append("}");
}
}
}