Ich möchte den Hintergrund des Textfelds ändern. Das ist mein Code:
@Html.TextBoxFor(p => p.Publishers[0].pub_name)
Was muss ich noch in TextBoxFor schreiben, um den Hintergrund zu ändern?
Bei einer Überladung der TextBoxFor-Methode können Sie ein Objekt für die HTML-Attribute übergeben.
@Html.TextBoxFor(p => p.Publishers[0].pub_name, new { Class="YourBackgroundClass" })
Dann haben Sie eine CSS-Regel wie:
.YourBackgroundClass { background:#cccccc; }
Wenn Sie einen Stil direkt anwenden möchten, können Sie Folgendes tun:
@Html.TextBoxFor(p => p.Publishers[0].pub_name, new { Style="background:#cccccc;" })
In meinem Fall habe ich unten gefallen. Ich habe eine ASP.NET MVC-Anwendung und wir verwenden Bootstrap. Ich gab Float: Links zu all meinen Div Elementen. Ich wollte nur zeigen, wie Sie @style zusammen mit @class für @ Html.TextBoxFor verwenden können.
<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>
Jetzt können Sie HTML-Attribute wie folgt hinzufügen:
@Html.EditorFor(p => p.Publishers[0].pub_name, new { htmlAttributes = new { @class =
"YourBackgroundClass" } })