Html.Textbox验证CSS未应用

时间:2009-12-30 19:04:01

标签: asp.net-mvc validation textbox

提前感谢您的帮助。我在我创建的两个网站上遇到了问题。我在页面上有Html.TextBox需要填写,我有验证工作,以便验证消息显示为空。我的问题是TextBox本身的CSS。由于某种原因,文本框不会应用.input-validation-error CSS样式,但Html.TextArea会!当文本框中只允许15或20个字符时,使用TextAreas是不切实际的。

然后TextBoxes和它们一起使用的ValidationMessages具有相同的名称。

我很难过。

谢谢, 蒂姆萨维奇 Web开发人员 ACEP,LLC

3 个答案:

答案 0 :(得分:0)

如果是这样的话,HTML文本框的HTML看起来像

<input type='text' name='something'></input>

而textarea看起来像

<textarea name='something'></textarea>

你确定CSS是正确的吗?一些示例代码可能有帮助......

答案 1 :(得分:0)

嘿谢谢你的信息。以下是有关此问题的一些代码。首先是一些背景:我正在使用包含contactName,contactAddress,contactMessage(它是电子邮件应用程序)等信息的View Model。视图包含一个TextBox,用于输入contactName或contactAddress以及用于该消息的TextArea。

如果contactName TextBox为空或包含无效的电子邮件地址,则会向ModelState添加一条错误消息,并显示ValidationMessage(仅包含一个星号(*))。 TextArea(以相同的方式设置)显示星号并且也变为红色并且具有红色边框但TextBox不会。


以下是一些代码:

查看模型:
    命名空间SendAPage.Models.ViewModels     {         公共类SendMailViewModel         {             public int Id {get;组; }             public string contactName {get;组; }             public string [] contactNames {get;组; }             public string contactAddress {get;组; }             public int propertyId {get;组; }             public string contactMessage {get;组; }

        public Property Property { get; set; }

        public SelectList PropertySelectList { get; set; }

        public string[] selectedContacts { get; set; }

        public bool IsValid { get { return (GetRuleViolations().Count() == 0); } }

        public IEnumerable<RuleViolation> GetRuleViolations()
        {
            if (string.IsNullOrEmpty(contactMessage))
                yield return new RuleViolation("The message cannot be empty", "contactMessage");
            if (string.IsNullOrEmpty(contactName) && (contactNames == null || contactNames.Count() == 0))
                yield return new RuleViolation("You must select a person to send to", "contactName");
            if(!Regex.IsMatch(contactName, @"^[a-zA-Z][\w\.-]*[a-zA-Z0-9]@[a-zA-Z0-9][\w\.-]*[a-zA-Z0-9]\.[a-zA-Z][a-zA-Z\.]*[a-zA-Z]$"))
                yield return new RuleViolation("The Email Address is not valid. Please enter a new one.", "contactName");
        }

    }
}

查看:
    &lt;%= Html.ValidationSummary(“有一些错误。请修复它们并重试。”)%&gt;     &LT;%                     使用(Html.BeginForm())                     {%>                     &lt;%= Html.Hidden(“Id”,Model.Id)%&gt;                     &lt;%= Html.Hidden(“Property”,Model.Property)%&gt;                     &lt;%= Html.Hidden(“propertyId”,Model.propertyId)%&gt;                                                                                                            选择收件人:                                                                                           输入消息:                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       &lt;%= Html.TextBox(“contactName”,Model.contactName)%&gt;                                                     &lt;%= Html.ValidationMessage(“contactName”,“”)%&gt;                                                                                                                                                                                                                                                                                                                                              (仅限A-Z&amp; 0-9允许250个字符最大计数)                                     
                                     输入的字符|剩下的字符                                                                   &lt;%= Html.TextArea(“contactMessage”,Model.contactMessage,8,10,new {@class =“word_count”,maxlength =“250”})%&gt;                                  &lt;%= Html.ValidationMessage(“contactMessage”,“
”)%&gt;                             

                    </tr>
                    <tr>
                        <td style="vertical-align: top; text-align: center;">
                            <input type="submit" value="Send Message" />&nbsp;<input type="reset" value="Reset Form" />
                        </td>
                    </tr>
                </table>
        <% } %>

CSS:

.field-validation-error
{
    color: #ff0000;
}

.input-validation-error
{
    border: 1px solid #ff0000;
    background-color: #ffeeee;
}

.validation-summary-errors
{
    font-weight: bold;
    padding:.8em;
    margin-bottom:1em;
    color:#8a1f11;
    border-color:#FBC2C4;
}

.error
{
 color: Red;
}

任何信息都将不胜感激。

谢谢,

蒂姆萨维奇(Tim Savage) Web开发人员
ACEP,LLC

答案 2 :(得分:0)

添加.input-validation-error和.input.input-validation-error类。