Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can ASP.NET MVC generate elements with lower case name and id attributes

Tags:

asp.net-mvc

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?

like image 298
Bryan Migliorisi Avatar asked Jul 19 '11 15:07

Bryan Migliorisi


People also ask

What is MVC Razor?

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.


1 Answers

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

like image 59
Muhammad Adeel Zahid Avatar answered Oct 18 '22 14:10

Muhammad Adeel Zahid