开发者

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

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 attrib开发者_如何学Pythonute 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?


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜