@html.validationmessagefor加样式,Html.ValidationMessageFor Text Color

问题

Probably a simple question, but i cant seem to find the answer.

using MVC 2 i have a series of Html.ValidationFor controls. I want to assign a CSS class to the text and cant seem to do it.

Model.Chest, new { @class = "textBoxMeasure" })%>

Model.Chest) %>

if i try the same method as textboxfor i get errors because it requires a string, when i put a string in it still wont work!

thanks

回答1:

There's a variant that takes htmlAttributes as the third argument (the second is the message that should be displayed, or you can use null for the default validation message)

Html.ValidationMessageFor(

Model => Model.Chest,

"Please enter a value",

new { @class = "redText" })

For more info see http://msdn.microsoft.com/en-us/library/ee721293%28v=vs.98%29.aspx

回答2:

I added a comment to the accepted answer, but I cannot to format it for better view. So, here is my already formatted comment from the accepted response.

I had similar case and I used solution from accepted answer. But I desired to use message from model annotation. I tried this:

@Html.ValidationMessageFor(Model => Model.Chest, null, new { @class = "text-danger" });

and it correctly worked for me. I used MVC4 with bootstrap.

回答3:

Use the classes assigned to the span tag holding the message. If the field is valid the class is field-validation-valid. If there is an error its field-validation-error.

I use

.field-validation-error {

color:Red;

}

回答4:

simplest way to do this to put @Html.ValidationMessageFor in div tag and apply css to div tag

@Html.ValidationMessageFor(model => model.Password, "", new { @class = "text-danger" })

来源:https://stackoverflow.com/questions/8438439/html-validationmessagefor-text-color

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值