Html.TextBoxFor格式和css类

Fil*_*lip 29 html.textbox asp.net-mvc-2

下面的两行代码工作正常,但我想将它们组合起来.我的意思是:我想在第一个代码行中使用@class.我怎样才能做到这一点?

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

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

谢谢,

菲利普

小智 66

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

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


Dar*_*rov 0

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

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

    <%: Html.EditorFor(x => x.Product) %>
    
    Run Code Online (Sandbox Code Playgroud)

    并在此编辑器模板内:

    <%: Html.TextBox("Price", string.Format("{0:f}", Model.Product.Price), new { @class = "textBox150" }) %>
    <%: Html.TextBoxFor(model => model.Product.Name, new { @class = "textBox150" })%>        
    
    Run Code Online (Sandbox Code Playgroud)
  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>
    
    Run Code Online (Sandbox Code Playgroud)

    然后修改你的CSS规则:

    .container150 input {
        // some rule that applies to the textbox
    }
    
    Run Code Online (Sandbox Code Playgroud)