【问题标题】:Html.TextBoxFor format and css classHtml.TextBoxFor 格式和 css 类
【发布时间】:2011-01-22 17:13:05
【问题描述】:

下面的两行代码工作正常,但我想将它们结合起来。 我的意思是:我想在第一行代码中使用@class。 我该怎么做?

<%: Html.TextBoxFor(model => model.Product.Price, String.Format("{0:f}", Model.Product.Price))%>

<%: Html.TextBoxFor(model => model.Product.Name, new { @class = "textBox150" })%>

谢谢,

菲利普

【问题讨论】:

    标签: asp.net-mvc-2 html.textbox


    【解决方案1】:

    我知道我来晚了,但我刚刚找到了解决这个问题的方法:

    <%: Html.TextBoxFor(model => model.StartDate, new { @class = "datepicker", Value=String.Format("{0:d}", Model.StartDate) })%>
    

    【讨论】:

    • 我花了很长时间才找到这个答案。谢谢!
    • @Cheddar - 谢谢你。这节省了我头上的一些头发!
    【解决方案2】:

    恐怕没有干净的方法来实现这一点。有两种可能性:

    1. 对 Product 属性使用编辑器模板:

      <%: Html.EditorFor(x => x.Product) %>
      

      在这个编辑器模板中:

      <%: Html.TextBox("Price", string.Format("{0:f}", Model.Product.Price), new { @class = "textBox150" }) %>
      <%: Html.TextBoxFor(model => model.Product.Name, new { @class = "textBox150" })%>        
      
    2. 编写一个将附加类的自定义助手

    3. 将这些文本框包装在 span:

      <span class="container150">
          <%: Html.TextBoxFor(model => model.Product.Price, String.Format("{0:f}", Model.Product.Price))%>
          <%: Html.TextBoxFor(model => model.Product.Name)%>
      </span>
      

      然后修改你的 CSS 规则:

      .container150 input {
          // some rule that applies to the textbox
      }
      

    【讨论】:

      【解决方案3】:

      再说一次,可能已经晚了,但我相信更好的解决方案是使用 jquery.maskedinput 插件(也可作为 nuget 包提供)。

      为什么使用插件比使用输入格式更好?好吧,当有人修改输入时,如果您不使用插件,您将失去格式。

      Here你可以找到它的用法和演示。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-09-20
        • 2011-04-15
        • 2011-01-06
        • 1970-01-01
        • 1970-01-01
        • 2013-05-18
        相关资源
        最近更新 更多