A form-entry Tag Helper | Passion for Coding
Composing line of work applications generally suggests developing a great deal of types for information entry. Composing the HTML for them over and over once again bores and likewise suggests copy-pasting the design structure into each and every single type. Copy-pasting works great as long as we one mores than happy with the style, however when it requires to be changed (beyond what’s possible by CSS), all types in the application requirement to alter. To treat this, I developed a form-entry tag assistant. Now developing an entry for a field in a type is as easy as << form-entry asp-for=" LocationName"/>>
Utilizing the default scaffolding in Visual Studio, I would get a type that duplicates the very same pattern over and over once again, for each residential or commercial property of the view design.
<< << << < << << << < << <<<< <<< type asp-action=" Produce"> <> < div asp-validation-summary=" ModelOnly"> |
output Material
AppendHtml( author ToString( ));} } . personal space WriteLabel( TextWriter author) { var tagBuilder = htmlGenerator GenerateLabel ( ViewContext,. For ModelExplorer,. For Call,. labelText : null ,. htmlAttributes: brand-new { @ class =" control-label" } ); . tagBuilder WriteTo ( author, htmlEncoder);} . personal space WriteInput ( TextWriter author) { |
tagBuilder =
htmlGenerator GenerateTextBox( ViewContext,. For
ModelExplorer
,.
For
Call,.
worth:
null
,. format : null,. htmlAttributes: brand-new { @ class = " form-control" } ); . tagBuilder WriteTo( author, htmlEncoder) ; } . personal space WriteValidation( TextWriter author) { var tagBuilder = htmlGenerator GenerateValidationMessage ( ViewContext,. For ModelExplorer,. For[HtmlAttributeName(ForAttributeName)] Call,. message : null,. tag : null ,. htmlAttributes: [HtmlAttributeNotBound] [ViewContext] brand-new { @ class = " text-danger"} ); . tagBuilder WriteTo( author, htmlEncoder); } } public class FormEntryTagHelper: TagHelper . { . personal readonly IHtmlGenerator htmlGenerator; . personal readonly HtmlEncoder htmlEncoder ; . . public FormEntryTagHelper( IHtmlGenerator htmlGenerator, HtmlEncoder htmlEncoder) . { . this.htmlGenerator = htmlGenerator; . this.htmlEncoder = htmlEncoder; . } . . personal const string ForAttributeName="asp-for"; . . . public ModelExpression For {get; set;} . . .(* ) . public ViewContext ViewContext { get; set;} . . public override space Process( TagHelperContext context, TagHelperOutput output) . { . output.TagName="div"; . output.TagMode= TagMode.StartTagAndEndTag; . output.Attributes.Add(" class", "form-group"); . . utilizing( var author = brand-new StringWriter()) . { . WriteLabel( author); . WriteInput( author) ; . WriteValidation( author); . output.Content.AppendHtml( writer.ToString()); .} .} . . personal space WriteLabel( TextWriter author) . { . var tagBuilder= htmlGenerator.GenerateLabel( . ViewContext, . For.ModelExplorer, . For.Name, . labelText: null, . htmlAttributes: brand-new { @class=" control-label"}); . . tagBuilder.WriteTo ( author, htmlEncoder); .} . . personal space WriteInput (TextWriter author) . { . var tagBuilder= htmlGenerator.GenerateTextBox( . ViewContext, . For.ModelExplorer, . For.Name, . worth: null, . format: null, . htmlAttributes: brand-new { @class=" form-control"} . ); . . tagBuilder.WriteTo( author, htmlEncoder) ; .} . . personal space WriteValidation( TextWriter author) . { . var tagBuilder= htmlGenerator.GenerateValidationMessage( . ViewContext, . For.ModelExplorer, . For.Name, . message: null, . tag: null, . htmlAttributes: brand-new { @class="text-danger"}); . . tagBuilder.WriteTo( author, htmlEncoder); . } . } . Published in . C#, Web on 2018-04-23|Tagged Asp.Net Core . . . . . |