Regex RegularExpressionAttribute不工作,我不';我不知道为什么
我有一个简单的MVC2视图模型类,它具有MagicItem属性:Regex RegularExpressionAttribute不工作,我不';我不知道为什么,regex,asp.net-mvc-2,attributes,Regex,Asp.net Mvc 2,Attributes,我有一个简单的MVC2视图模型类,它具有MagicItem属性: public class VoodooViewModel { [Required(AllowEmptyStrings = false, ErrorMessage = "The Magic Item is required")] [RegularExpression(@"^[^-]*$", ErrorMessage = "Hyphens are not
public class VoodooViewModel {
[Required(AllowEmptyStrings = false,
ErrorMessage = "The Magic Item is required")]
[RegularExpression(@"^[^-]*$",
ErrorMessage = "Hyphens are not allowed in Magic Items.")]
public string MajorModel { get; set; }
}
我只是试图在这个属性中不允许连字符,但就我个人而言,我无法让它工作。有人知道我做错了什么吗(RequiredAttribute工作正常)
在我看来,我使用的正则表达式是“从字符串的开头到结尾,匹配任何非连字符的内容”。我已经在Regex测试仪中测试过了,它可以工作——但在我的代码中没有。无论我在其中添加了多少连字符,我都无法显示错误。就像一个工具一样,我忘了检查控制器的操作方法以查看ModelState是否有效:
public ActionResult UberController(VoodooViewModel vvm)
{
if (!ModelState.IsValid) return View(vvm); //turns out this line is important
(...yaddayaddayadda...)
}
感谢您为我指明了正确的方向。就像一个工具一样,我忘了检查控制器的操作方法以查看ModelState是否有效:
public ActionResult UberController(VoodooViewModel vvm)
{
if (!ModelState.IsValid) return View(vvm); //turns out this line is important
(...yaddayaddayadda...)
}
感谢您为我指明了正确的方向。当我尝试此代码时,问题一定在其他地方。当我尝试此代码时,问题一定在其他地方。