How can I apply styling to asp.net mvc @Html.Textb

2019-03-11 21:30发布

I want to change the background of the textbox. This is my code:

@Html.TextBoxFor(p => p.Publishers[0].pub_name)

What more do I need to write in TextBoxFor to change the background?

3条回答
在下西门庆
2楼-- · 2019-03-11 21:53

An overload of the TextBoxFor method allows you to pass an object for the HTML attributes.

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

Then you can have a CSS rule such as:

.YourBackgroundClass { background:#cccccc; }

If you want to apply a style directly you can do:

@Html.TextBoxFor(p => p.Publishers[0].pub_name, new { Style="background:#cccccc;" })
查看更多
霸刀☆藐视天下
3楼-- · 2019-03-11 21:55

Now you can add html attributes like this:

 @Html.EditorFor(p => p.Publishers[0].pub_name, new { htmlAttributes = new { @class = 
 "YourBackgroundClass" } })
查看更多
爱情/是我丢掉的垃圾
4楼-- · 2019-03-11 22:07

In my case I did like below. I have ASP.NET MVC application and we are using Bootstrap. I gave float:left to all my div elements. Just wanted to show how you can use @style along with @class for @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>

enter image description here

查看更多
登录 后发表回答