, :
- (
IHtmlGenerator) - (
Tag Helpers)
asp-validation-summary asp-validation-for GenerateValidationSummary GenerateValidationMessage IHtmlGenerator, DefaultHtmlGenerator.
, DefaultHtmlGenerator , . , .
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc();
services.AddTransient<IHtmlGenerator, MyHtmlGenerator>();
}
DefaultHtmlGenerator, .
- IHtmlGenerator
, , . , , ConfigureServices, .
using Microsoft.AspNetCore.Antiforgery;
using Microsoft.AspNetCore.Html;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.ModelBinding;
using Microsoft.AspNetCore.Mvc.Rendering;
using Microsoft.AspNetCore.Mvc.Routing;
using Microsoft.AspNetCore.Mvc.ViewFeatures;
using Microsoft.AspNetCore.Mvc.ViewFeatures.Internal;
using Microsoft.Extensions.Options;
using System.Text.Encodings.Web;
namespace ValidationSampleWebApplication
{
public class MyHtmlGenerator : DefaultHtmlGenerator
{
public MyHtmlGenerator(IAntiforgery antiforgery, IOptions<MvcViewOptions> optionsAccessor, IModelMetadataProvider metadataProvider, IUrlHelperFactory urlHelperFactory, HtmlEncoder htmlEncoder, ValidationHtmlAttributeProvider validationAttributeProvider)
: base(antiforgery, optionsAccessor, metadataProvider, urlHelperFactory, htmlEncoder, validationAttributeProvider)
{
}
public override TagBuilder GenerateValidationMessage(ViewContext viewContext, ModelExplorer modelExplorer, string expression, string message, string tag, object htmlAttributes)
{
return base.GenerateValidationMessage(viewContext, modelExplorer, expression, message, tag, htmlAttributes);
}
public override TagBuilder GenerateValidationSummary(ViewContext viewContext, bool excludePropertyErrors, string message, string headerTag, object htmlAttributes)
{
return base.GenerateValidationSummary(viewContext, excludePropertyErrors, message, headerTag, htmlAttributes);
}
}
}
. TagHelper Process.
_ViewImports.cshtml:
@using ValidationSampleWebApplication
@using ValidationSampleWebApplication.Models
@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers
@addTagHelper *, ValidationSampleWebApplication
:
- hasError div
asp-myvalidation-for, div <div class="form-group" asp-myvalidation-for="LastName"> hasError div, . _ViewImports.cshtml, .
using Microsoft.AspNetCore.Mvc.Rendering;
using Microsoft.AspNetCore.Mvc.ViewFeatures;
using Microsoft.AspNetCore.Razor.TagHelpers;
using Microsoft.AspNetCore.Mvc.TagHelpers;
using Microsoft.AspNetCore.Mvc.ModelBinding;
namespace ValidationSampleWebApplication
{
[HtmlTargetElement("div", Attributes = MyValidationForAttributeName)]
public class MyValidationTagHelper : TagHelper
{
private const string MyValidationForAttributeName = "asp-myvalidation-for";
[HtmlAttributeNotBound]
[ViewContext]
public ViewContext ViewContext { get; set; }
[HtmlAttributeName(MyValidationForAttributeName)]
public ModelExpression For { get; set; }
public override void Process(TagHelperContext context, TagHelperOutput output)
{
base.Process(context, output);
ModelStateEntry entry;
ViewContext.ViewData.ModelState.TryGetValue(For.Name, out entry);
if (entry != null && entry.Errors.Count > 0)
{
var builder = new TagBuilder("div");
builder.AddCssClass("hasError");
output.MergeAttributes(builder);
}
}
}
}
- field-validation-error div
div asp-validation-for. div. div asp-validation-for, field-validation-error, , div field-validation-valid.
, , . , helper span span, div, , div. _ViewImports.cshtml, .
using Microsoft.AspNetCore.Mvc.ViewFeatures;
using Microsoft.AspNetCore.Razor.TagHelpers;
using Microsoft.AspNetCore.Mvc.TagHelpers;
namespace ValidationSampleWebApplication
{
[HtmlTargetElement("div", Attributes = ValidationForAttributeName)]
public class MytValidationMessageTagHelper : ValidationMessageTagHelper
{
private const string ValidationForAttributeName = "asp-validation-for";
public MytValidationMessageTagHelper(IHtmlGenerator generator) : base(generator)
{
}
}
}