样式的HTML帮助ASP。NET MVC

时间:2022-05-19 21:08:28

If I have an HTML helper like so:

如果我有一个像这样的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?

如何对它应用CSS类?我要把它包成一个跨度吗?或者我需要以某种方式利用helper的HtmlAttributes属性吗?

5 个解决方案

#1


33  

You can pass it into the TextBox call as a parameter.

您可以将它作为参数传递给文本框调用。

Name:<br/>    
<%= Html.TextBox("txtName", "20", new { @class = "hello" }) %>

This line will create a text box with the value 20 and assign the class attribute with the value hello. I put the @ character in front of the class, because class is a reserved keyword. If you want to add other attributes, just separate the key/value pairs with commas.

这一行将创建一个带有值20的文本框,并使用值hello分配class属性。我将@字符放在类前面,因为类是一个保留的关键字。如果想添加其他属性,只需使用逗号分隔键/值对。

#2


6  

This is how to add a class and a style on the same element...

这就是如何在同一个元素上添加类和样式……

"x" being the model passed to the view with a property of TextBoxID

“x”是传递给具有TextBoxID属性的视图的模型

@Html.TextBoxFor(x => x.TextBoxID, new { @class = "SearchBarSelect", style = "width: 20px; background-color: green;" })

#3


2  

I did some research and came across this article that seems to have a solution to your question.

我做了一些研究,发现了这篇文章,似乎可以解决你的问题。

Ajax Control Toolkit with ASP.NET MVC#

使用ASP的Ajax控件工具包。NET MVC #

source: jimzimmerman

来源:jimzimmerman

ARTICLE LINK

文章链接

http://www.ajaxprojects.com/ajax/tutorialdetails.php?itemid=330

http://www.ajaxprojects.com/ajax/tutorialdetails.php?itemid=330

QUOTE

报价

So basically if you put the class name TextboxWatermark on any textbox input with the title you like to show as the watermark like this:

基本上,如果你把类名TextboxWatermark加到任何文本框中你想要显示的标题就像这样:

<input type="text" class"TextboxWatermark" name="username" id="username" title="Must be at least 6 chars" />

or

<%= Html.TextBox("username", new { @class = "TextboxWatermark", @title = "Must be at least 6 chars" }) %>

What is nice about the second option is that you get the added benefit of getting the View Engine to fill out the value of the textbox if there is an item in ViewData of the ViewData.Model that has a var named 'username'.

第二个选项的好处是,如果ViewData的ViewData中有项,您可以通过让视图引擎填写文本框的值来获得额外的好处。有一个名为“用户名”的var模型。

#4


2  

Use the htmlAttributes parameter with an anonymous type, like tihs:

使用带有匿名类型的htmlAttributes参数,如tihs:

<%=Html.TextBox("txtName","20", new { @class = "test"}) %>

#5


-1  

Is it that much more work?

还有那么多工作吗?

#1


33  

You can pass it into the TextBox call as a parameter.

您可以将它作为参数传递给文本框调用。

Name:<br/>    
<%= Html.TextBox("txtName", "20", new { @class = "hello" }) %>

This line will create a text box with the value 20 and assign the class attribute with the value hello. I put the @ character in front of the class, because class is a reserved keyword. If you want to add other attributes, just separate the key/value pairs with commas.

这一行将创建一个带有值20的文本框,并使用值hello分配class属性。我将@字符放在类前面,因为类是一个保留的关键字。如果想添加其他属性,只需使用逗号分隔键/值对。

#2


6  

This is how to add a class and a style on the same element...

这就是如何在同一个元素上添加类和样式……

"x" being the model passed to the view with a property of TextBoxID

“x”是传递给具有TextBoxID属性的视图的模型

@Html.TextBoxFor(x => x.TextBoxID, new { @class = "SearchBarSelect", style = "width: 20px; background-color: green;" })

#3


2  

I did some research and came across this article that seems to have a solution to your question.

我做了一些研究,发现了这篇文章,似乎可以解决你的问题。

Ajax Control Toolkit with ASP.NET MVC#

使用ASP的Ajax控件工具包。NET MVC #

source: jimzimmerman

来源:jimzimmerman

ARTICLE LINK

文章链接

http://www.ajaxprojects.com/ajax/tutorialdetails.php?itemid=330

http://www.ajaxprojects.com/ajax/tutorialdetails.php?itemid=330

QUOTE

报价

So basically if you put the class name TextboxWatermark on any textbox input with the title you like to show as the watermark like this:

基本上,如果你把类名TextboxWatermark加到任何文本框中你想要显示的标题就像这样:

<input type="text" class"TextboxWatermark" name="username" id="username" title="Must be at least 6 chars" />

or

<%= Html.TextBox("username", new { @class = "TextboxWatermark", @title = "Must be at least 6 chars" }) %>

What is nice about the second option is that you get the added benefit of getting the View Engine to fill out the value of the textbox if there is an item in ViewData of the ViewData.Model that has a var named 'username'.

第二个选项的好处是,如果ViewData的ViewData中有项,您可以通过让视图引擎填写文本框的值来获得额外的好处。有一个名为“用户名”的var模型。

#4


2  

Use the htmlAttributes parameter with an anonymous type, like tihs:

使用带有匿名类型的htmlAttributes参数,如tihs:

<%=Html.TextBox("txtName","20", new { @class = "test"}) %>

#5


-1  

Is it that much more work?

还有那么多工作吗?