htmlAttributes:如何为下面添加CSS类规则?

时间:2010-11-15 04:32:14

标签: css model-view-controller asp.net-mvc-2

我想添加如下的css类语句:

<%: Html.TextBoxFor(model => model.Comments, new { @class = "lookandfeel_1" })%>

到这一行:

<%: Html.TextBoxFor(model => model.Money, String.Format("{0:F}", Model.Money)) %>

我该怎么做?

1 个答案:

答案 0 :(得分:1)

尝试使用“显示格式”属性修饰属性:

//Your view object
[DisplayFormat( DataFormatString= "{0:F}" )]
public Double Money { get; set; }

//Your view code
<%: Html.TextBoxFor(model => model.Money,  new { @class = "lookandfeel_1" }) %>

如果您使用的是Entity Framework或类似的,则需要执行以下操作来添加元数据属性:

    using System.ComponentModel.DataAnnotations;

    namespace MyNameSpace
    {

        [MetadataType(typeof(MetaDataProduct))]
        public partial class MyEntityClass
        {

        }

        public class MetaDataProduct
        {       
           [DisplayName("Price")]
           [DisplayFormat( DataFormatString= "{0:F}" )]
           public int Money{ get; set; }
        }
    }