ASP.NET MVC可以产生小写的名称和ID属性的元素 [英] Can ASP.NET MVC generate elements with lower case name and id attributes

查看:99
本文介绍了ASP.NET MVC可以产生小写的名称和ID属性的元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我恨ASP.NET HTML佣工在同一案件中为POCO的属性生成名称和id属性。

I hate that ASP.NET html helpers generates name and id attributes in the same case as the POCO properties.

大多数开发者通常使用小写值名称和ID,但我似乎无法找到一种方法,在ASP.NET MVC中做到这一点。

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.

这是在所有可能的?

修改
我会更具体,什么我谈论,因为似乎有一些混乱的例子。

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

我的模型是这样的:

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

在我看来,我使用HTML herlpers像这样:

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.

我不希望改变POCO属性名称是小写。这么多的ASP.NET MVC框架是可扩展的,可定制的 - 这是东西是不是

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?

推荐答案

显示(NAME =ABC)属性更改Html.LabelFor的输出,当我们使用EditorForModel和DisplayForModel。它不会影响Html.TextBoxFor的呈现方式,它不应该改变表单字段的ID或名称属性的呈现方式。如果你真的需要的话,你必须编写自己的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)

注意,这个例子,是刚生成的id和name属性。你将不得不code像不显眼的属性,这种方法甚至其他重载有效地使用它的其他属性。

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

这篇关于ASP.NET MVC可以产生小写的名称和ID属性的元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆