Can ASP.NET MVC generate elements with the smallest identifier names and attributes

I hate ASP.NET html helpers generate name and identifier attributes in the same case as POCO properties.

Most developers typically use lowercase values ​​for name and identifier, but I cannot find a way to do this in ASP.NET MVC.

Looking at the source shows that there is no way to override this functionality, but I could be wrong.

Is it possible?

EDIT : I will be more specific, with examples of what I'm talking about, since there seems to be some kind of confusion.

My model looks like this:

public class Customer { public string FirstName { get; set; } public string LastName { get; set; } } 

In my opinion, I use Html herlpers as follows:

 @Html.InputFor(m => m.FirstName) 

This creates markup that looks like this:

 <input type="text" name="FirstName" id="FirstName" value="" /> 

I hate that the FirstName property is capitalized. In an ideal world, I would like to override the generation of this attribute value so that I can force it to say "firstName" instead.

I do not want to change the POCO property names to lowercase. So most of the ASP.NET MVC framework is extensible and customizable - is it something that isn't there?

+6
source share
1 answer
Attribute

Display(Name = "abc") changes the output of Html.LabelFor and when we use EditorForModel and DisplayForModel. this does not affect how the Html.TextBoxFor is rendered, and it should not change the way the identifier attributes or the form field name is displayed. if you really need to do this, you need to write your own html helper, for example

 public static MvcHtmlString LowerTextBoxFor<TModel,TProperty>(this HtmlHelper<TModel> html, Expression<Func<TModel,TProperty>> expr) { StringBuilder result = new StringBuilder(); TagBuilder tag = new TagBuilder("input"); tag.MergeAttribute("type","text"); var lowerPropertyName = ExpressionHelper.GetExpressionText(expr).ToLower(); tag.MergeAttribute("name",lowerPropertyName); tag.MergeAttribute("id",lowerPropertyName); result.Append(tag.ToString()); return new MvcHtmlString(result.ToString()); } 

you can use this html helper in view.

 @Html.LowerTextBoxFor(x=>x.Property1) 

Remember that this example only generates the id and name attribute. you will have to encode other attributes, such as unobtrusive attributes and even other overloads of this method to use it effectively

+1
source

Source: https://habr.com/ru/post/893082/


All Articles