您好,谢谢您的信息。这是有关此问题的一些代码。首先是一些背景知识:我正在使用一个包含诸如contactName、contactAddress、contactMessage 等信息的视图模型(它是一个电子邮件应用程序)。 View 包含一个用于输入contactName 或contactAddress 的TextBox 和一个用于消息的TextArea。
如果 contactName 文本框为空或包含无效的电子邮件地址,则会将错误消息添加到 ModelState 并显示 ValidationMessage(仅包含星号 (*))。 TextArea(设置方式相同)显示星号,也会变成红色并带有红色边框,但 TextBox 没有。
这里有一些代码:
查看模型:
命名空间 SendAPage.Models.ViewModels
{
公共类 SendMailViewModel
{
公共 int ID { 获取;放; }
公共字符串联系人姓名 { 获取;放; }
公共字符串 [] 联系人姓名 { 获取;放; }
公共字符串联系人地址 { 获取;放; }
公共 int propertyId { 获取;放; }
公共字符串contactMessage {获取;放; }
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");
}
}
}
查看:
选择收件人:
输入留言:
") %>
(仅 A-Z 和 0-9 允许 250 个字符。最大计数)
输入的字符 |剩余字符
") %>
</tr>
<tr>
<td style="vertical-align: top; text-align: center;">
<input type="submit" value="Send Message" /> <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;
}
任何信息将不胜感激。
谢谢你,
蒂姆·萨维奇
网页开发者
ACEP,有限责任公司