私がそのようなHTMLヘルパーを持っている場合:
Name:<br />
<%=Html.TextBox("txtName",20) %><br />
How do I apply a CSS class to it? Do I have to wrap it in a span? Or do I need to somehow utilize the HtmlAttributes property of the helper?
私がそのようなHTMLヘルパーを持っている場合:
Name:<br />
<%=Html.TextBox("txtName",20) %><br />
How do I apply a CSS class to it? Do I have to wrap it in a span? Or do I need to somehow utilize the HtmlAttributes property of the helper?
これをパラメーターとしてTextBox呼び出しに渡すことができます。
Name:<br/>
<%= Html.TextBox("txtName", "20", new { @class = "hello" }) %>
この行は、値20のテキストボックスを作成し、クラス属性に値helloを割り当てます。クラスは予約語であるため、クラスの前に@文字を付けました。他の属性を追加する場合は、キーと値のペアをコンマで区切ります。
これは、同じ要素にクラスとスタイルを追加する方法です...
「x」は、TextBoxIDのプロパティを持つビューに渡されるモデルです。
@Html.TextBoxFor(x => x.TextBoxID, new { @class = "SearchBarSelect", style = "width: 20px; background-color: green;" })
私はいくつかの調査を行い、あなたの質問に対する解決策があると思われるこの記事に出くわしました.
ASP.NET MVC を使用した Ajax コントロール ツールキット#
ソース: ジムジマーマン
記事リンク
http://www.ajaxprojects.com/ajax/tutorialdetails.php?itemid=330
見積もり
したがって、基本的に、クラス名 TextboxWatermark を、次のように透かしとして表示したいタイトルのテキストボックス入力に配置すると、次のようになります。
<input type="text" class"TextboxWatermark" name="username" id="username" title="Must be at least 6 chars" />
また
<%= Html.TextBox("username", new { @class = "TextboxWatermark", @title = "Must be at least 6 chars" }) %>
2 番目のオプションの優れている点は、ViewData.Model の ViewData に「username」という名前の var を持つ項目がある場合、View Engine にテキスト ボックスの値を入力させるという追加の利点が得られることです。
htmlAttributes
tihsのように、匿名タイプのパラメーターを使用します。
<%=Html.TextBox("txtName","20", new { @class = "test"}) %>
動的ではないため、スパンを使用する必要はありません。
CSS:
.testClass {
color: #1600d3;
}
ビュー (インデックス):
@Html.TextBox("expression", "Text to show.", new { @class = "testClass" })
動的オプションが必要な場合は、たとえば次のように使用できます。
CSS:
.test class{
background: #ffffff;
}
コントローラー (テスト用インデックス):
[HttpGet]
public ActionResult Index()
{
ViewBag.vbColor = "#000000";
return View();
}
ビュー (インデックス):
<div>
<span>
@Html.TextBox("expression", "Text to show.", new
{ @class = "testClass", @style="color: " +
@ViewBag.vbColor })
</span>
</div>
それが役に立てば幸い。
そんなに仕事多いの?