I hate that ASP.NET html helpers generates name and id attributes in the same case as the POCO properties.
Most developers typically use lower case values for name and id but I cannot seem to find a way to accomplish this in ASP.NET MVC.
A look through the source shows that there is no way to override this functionality, but I could be wrong.
Is this at all possible?
EDIT: I'll be more specific, with examples of what I am talking about since there seems to be some confusion.
My model looks like this:
public class Customer
{
public string FirstName { get; set; }
public string LastName { get; set; }
}
In my view I use Html herlpers like so:
@Html.InputFor(m => m.FirstName)
This generates markup that looks like this:
<input type="text" name="FirstName" id="FirstName" value="" />
I hate that the FirstName property is capitalized. In a perfect world, I would like a way to override the generation of this attribute value such that I can make it say "firstName" instead.
I do not want to change the POCO property names to be lower case. So much of the ASP.NET MVC framework is extensible and customizable - is this something that is not?
Razor is a markup syntax that lets you embed server-based code into web pages using C# and VB.Net. It is not a programming language. It is a server side markup language. Razor has no ties to ASP.NET MVC because Razor is a general-purpose templating engine. You can use it anywhere to generate output like HTML.
Display(Name = "abc")
attribute changes the output of Html.LabelFor and when we use EditorForModel and DisplayForModel. it does not affect how Html.TextBoxFor is rendered and it is not supposed to change the way id or name attributes of form fields are rendered. if you really need to do so you have to write your own html helper like
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 like.
@Html.LowerTextBoxFor(x=>x.Property1)
Be aware that this example just generate just id and name attribute. you will have to code for other attributes like unobtrusive attributes and even other overloads of this method to effectively use it
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With