如何将样式应用于asp.net mvc @ Html.TextboxFor?

TCM*_*TCM 31 c# asp.net-mvc razor

我想更改文本框的背景.这是我的代码:

@Html.TextBoxFor(p => p.Publishers[0].pub_name)
Run Code Online (Sandbox Code Playgroud)

我还需要在TextBoxFor中编写更改背景的内容吗?

med*_*g15 51

TextBoxFor方法的重载允许您传递HTML属性的对象.

@Html.TextBoxFor(p => p.Publishers[0].pub_name, new { Class="YourBackgroundClass" })
Run Code Online (Sandbox Code Playgroud)

然后你可以有一个CSS规则,如:

.YourBackgroundClass { background:#cccccc; }
Run Code Online (Sandbox Code Playgroud)

如果您想直接应用样式,您可以:

@Html.TextBoxFor(p => p.Publishers[0].pub_name, new { Style="background:#cccccc;" })
Run Code Online (Sandbox Code Playgroud)

  • @Anthony:我修复了样本,它应该是`@ class`,因为`class`确实是一个保留的关键字. (3认同)
  • "前景"属性不起作用,因为它不是HTML属性,您必须将思路与"webforms"思维方式脱节,这些属性是纯HTML.如果您不想使用类,可以使用"样式"属性使用内联样式. (2认同)

Zig*_*ler 8

就我而言,我确实喜欢以下内容。我有ASP.NET MVC应用程序,我们正在使用Bootstrap。我给所有的div元素赋予了float:left。只是想展示如何将@style和@class一起用于@ Html.TextBoxFor

<div class="modal-body" style="height:auto; overflow-y:auto;max-height:500px;">
  <table style="width:100%" cellpadding="10">
     <tr>
       <td>
         <div style="display: inline; ">
            <label style=" float:left; margin-right:20px;">Login Name: </label>
            @Html.TextBoxFor(m => Model.UserPrincipalName, new { @id = "LoginName", @class = "form-control", @style = "float:left;margin-right:10px;margin-top:-5px;" })
            <a href="#" onclick="SearchActiveDirectoryByLoginName()" title="Search Active Directory" class="btn btn-primary" style="float: left; margin-top: -5px;">
                  @Html.Raw(" Search ")
             </a>
         </div>
       </td>
      </tr>
  </table>
</div>
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明