html – 如何让asp-for输入标签helper生成camelCase名称?

前端之家收集整理的这篇文章主要介绍了html – 如何让asp-for输入标签helper生成camelCase名称?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

如果我有这样的视图模型:


  1.  public class MyModel{
  2.       public DateTime? StartDate {get;set;}
  3.  }

在视图上,输入标记与asp-for标记助手一起使用,如下所示:


  1. <input asp-for="StartDate" />

由此生成的默认html是


  1.  <input type="datetime" id="StartDate" name="StartDate" value="" />

但我希望它生成的是html,如下所示:


  1.  <input type="datetime" id="startDate" name="startDate" value="" />

如何让asp-for输入标签帮助器生成像上面这样的驼峰案例名称,而不必使我的模型属性为camelCase?

最佳答案

在研究了@Bebben发布的代码及其提供的链接后,我继续深入研究Asp.Net Core源代码.我发现Asp.Net Core的设计者提供了一些可扩展性点,可用于实现更低的camelCase id和名称值.

为此,我们需要实现自己的IHtmlGenerator,我们可以通过创建一个继承自DefaultHtmlGenerator的自定义类来实现.然后在该类上我们需要覆盖GenerateTextBox方法来修复外壳.或者我们可以覆盖GenerateInput方法来修复所有输入字段(不仅仅是输入文本字段)的name和id属性值的大小写,这是我选择做的.作为奖励,我还覆盖了GenerateLabel方法,因此标签属性也使用自定义套管指定值.

这是班级:


  1.     using Microsoft.AspNetCore.Antiforgery;
  2.     using Microsoft.AspNetCore.Mvc;
  3.     using Microsoft.AspNetCore.Mvc.Internal;
  4.     using Microsoft.AspNetCore.Mvc.ModelBinding;
  5.     using Microsoft.AspNetCore.Mvc.Rendering;
  6.     using Microsoft.AspNetCore.Mvc.Routing;
  7.     using Microsoft.AspNetCore.Mvc.ViewFeatures;
  8.     using Microsoft.Extensions.Options;
  9.     using System.Collections.Generic;
  10.     using System.Text.Encodings.Web;
  11.  
  12.     namespace App.Web {
  13.         public class CustomHtmlGenerator : DefaultHtmlGenerator {
  14.  
  15.             public CustomHtmlGenerator(
  16.                 IAntiforgery antiforgery,IOptions<MvcViewOptions> optionsAccessor,IModelMetadataProvider MetadataProvider,IUrlHelperFactory urlHelperFactory,HtmlEncoder htmlEncoder,ClientValidatorCache clientValidatorCache) : base
  17.                                 (antiforgery,optionsAccessor,MetadataProvider,urlHelperFactory,htmlEncoder,clientValidatorCache) {
  18.  
  19.                //Nothing to do
  20.  
  21.             }
  22.  
  23.             public CustomHtmlGenerator(
  24.                 IAntiforgery antiforgery,ClientValidatorCache clientValidatorCache,ValidationHtmlAttributeProvider validationAttributeProvider) : base
  25.                                 (antiforgery,clientValidatorCache,validationAttributeProvider) {
  26.  
  27.                 //Nothing to do
  28.  
  29.             }
  30.  
  31.  
  32.             protected override TagBuilder GenerateInput(
  33.                 ViewContext viewContext,InputType inputType,ModelExplorer modelExplorer,string expression,object value,bool useViewData,bool isChecked,bool setId,bool isExplicitValue,string format,IDictionary<string,object> htmlAttributes) {
  34.  
  35.                 expression = GetLowerCamelCase(expression);
  36.  
  37.                 return base.GenerateInput(viewContext,inputType,modelExplorer,expression,value,useViewData,isChecked,setId,isExplicitValue,format,htmlAttributes);
  38.             }
  39.  
  40.  
  41.             public override TagBuilder GenerateLabel(
  42.                 ViewContext viewContext,string labelText,object htmlAttributes) {
  43.  
  44.                 expression = GetLowerCamelCase(expression);
  45.  
  46.                 return base.GenerateLabel(viewContext,labelText,htmlAttributes);
  47.             }
  48.  
  49.  
  50.             private string GetLowerCamelCase(string text) {
  51.  
  52.                 if (!string.IsNullOrEmpty(text)) {
  53.                     if (char.IsUpper(text[0])) {
  54.                         return char.ToLower(text[0]) + text.Substring(1);
  55.                     }
  56.                 }
  57.  
  58.                 return text;
  59.             }
  60.  
  61.         }
  62.     }

现在我们有了CustomHtmlGenerator类,我们需要在IoC容器中注册它来代替DefaultHtmlGenerator.我们可以通过以下两行在Startup.cs的ConfigureServices方法中执行此操作:


  1.   //Replace DefaultHtmlGenerator with CustomHtmlGenerator
  2.   services.Remove<IHtmlGenerator,DefaultHtmlGenerator>();
  3.   services.AddTransient<IHtmlGenerator,CustomHtmlGenerator>();

很酷.我们不仅解决了输入字段中的id和名称外壳问题,而且通过实现我们自己的自定义IHtmlGenerator,并使其注册,我们打开了可以完成的各种html定制的大门.

我开始非常欣赏围绕IoC构建的系统的强大功能,以及使用虚拟方法的默认类.在这种方法下轻松获得的定制水平真的非常惊人.

更新
@ Gup3rSuR4c指出我的services.Remove调用必须是一个未包含在框架中的扩展方法.我检查了,是的,这是真的.所以,这是该扩展方法代码


  1.  public static class IServiceCollectionExtensions {
  2.  
  3.     public static void Remove<TServiceType,TImplementationType>(this IServiceCollection services) {
  4.  
  5.         var serviceDescriptor = services.First(=> s.ServiceType == typeof(TServiceType) &&
  6.                                                     s.ImplementationType == typeof(TImplementationType));
  7.         services.Remove(serviceDescriptor); 
  8.     }
  9.  
  10. }

猜你在找的HTML相关文章